简体   繁体   中英

How to direct output from a Powershell command into a CMD variable

I am using PowerShell to get the File Version of a file

C:\\WINDOWS\\system32>powershell (Get-Command C:\\Users\\lafarnum\\Downloads\\aniteloggingclientapi.dll).FileVersionInfo.FileVersion 1.12.0.4

The output file version is 1.12.0.4. I wanted to pipe this file version into a DOS CMD variable like a normal assignment but the issue is it is simply assigning the PowerShell command string , not the output of the PowerShell command

C:\\WINDOWS\\system32>set pfpathset=powershell (Get-Command C:\\Users\\lafarnum\\Downloads\\aniteloggingclientapi.dll).FileVersionInfo.FileVersion

C:\\WINDOWS\\system32>echo %pdpathset% %pdpathset%

C:\\WINDOWS\\system32>echo %pfpathset% powershell (Get-Command C:\\Users\\lafarnum\\Downloads\\aniteloggingclientapi.dll).FileVersionInfo.FileVersion

C:\\WINDOWS\\system32>

So the PS command output does not seem to be directly directed to the CMD STDOUT output? So do I have to pipe the output of the PS command into another string variable and then assign that variable to my pfpathset variable? or do I have to use a ps script to do this?

Sorry if this is a basic question but I am a powershell novice, so any help given would be appreciated.

Thanking you in anticipation

Similar to Assign command output to variable in batch file . You need the double quotes because of the parentheses.

In a .bat file put %%i instead of %i.

for /f %i in ('powershell "(Get-Command notepad).FileVersionInfo.FileVersion"') do set output=%i

echo %output%
10.0.19041.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM