Batch can't import string from file and associate to var

Viewed 59

I'm trying associate Google Chrome version to variable in my script in batch, but

powershell -Command "& {(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion } > chromever.txt
set /p version=<chromever.txt
echo %version%
pause

text in file is: 97.0.4692.71 but console output return: ■9 ■9

Please help...

2 Answers

The immediate fix is to use Set-Content rather than >:

powershell -Command "(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion | Set-Content chromever.txt"

Note that I've removed "& { ... }" around your -Command argument, because it is never necessary - just use "..." directly. Older versions of the CLI documentation erroneously suggested that & { ... } is required, but this has since been corrected.

The problem is that > in Windows PowerShell (powershell.exe) produces "Unicode"-encoded (UTF-16LE-encoded) files by default, which cmd.exe doesn't decode properly (by default). Set-Content defaults to ANSI encoding (a superset of ASCII), and since you're only retrieving a version number composed of ASCII-range-characters only, no encoding issues should arise.


Alternatively, you could use a for /f loop to parse PowerShell's output directly, without the need to save to a temporary file:

for /f "usebackq delims=" %%i in (`
  powershell -c "(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion"
`) do set version=%%i

echo [%version%]

I would say, before you attempt to claim that there is a problem with your output, you should consider posting a properly complete commandline.

This has imbalanced doublequotes:

powershell -Command "& {(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion } > chromever.txt

Had you actually included the missing doublequote, and done so in the correct location, you wouldn't have received output from PowerShell in Unicode (UTF-16 LE).

Incorrect Location:

powershell -Command "& {(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion } > chromever.txt"

Had you placed it correctly, the output would have been in ANSI:

Correct Location:

powershell -Command "& {(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion }" > chromever.txt

Essentially because you've missed that doublequote off, it was effectively closed at the end of the line, (the redirection > was being done in PowerShell). Hence your result was returned in UTF-16 LE, instead of the required ANSI encoding ,(when the redirection is done in cmd.exe).


Personally however, I'd have done it like this:

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -Command "(Get-Item $(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion" 1>"chromever.txt"
@Set /P "version=" 0<"chromever.txt"
@Echo(%version%

And If I didn't actually need the text file:

@For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -Command "(Get-Item $(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion" 2^>NUL') Do @Set "version=%%G"
@Echo(%version%
Related