I want get value of an exported powershell variable in a batch script. Below are the sample scripts.
$myname="user1"
@echo on
FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"; (Get-Variable myname).value') DO SET VAL=%%i
echo %VAL%
pause
While executing sample.bat, I am always getting below error.
.value') was unexpected at this time.
But, if I execute like below in powershell I am able to get proper output.
. "D:\sample.ps1"; (Get-Variable myname).value
I want to know how to escape the parenthesis around Get-Variable command in batch script. Or would like to know any other way to get the value of exported powershell variable in batch script.
I can't test this without more information, but see if this helps:
@echo on
FOR /F "delims=" %%i IN ('"powershell . "D:\sample.ps1"; (Get-Variable myname).value"') DO SET VAL=%%i
echo %VAL%
pause
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With