Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Powershell variable value in batch script

I want get value of an exported powershell variable in a batch script. Below are the sample scripts.

sample.ps1

$myname="user1"

sample.bat

@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.

like image 414
vareda Avatar asked Mar 21 '23 10:03

vareda


1 Answers

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
like image 193
foxidrive Avatar answered Mar 31 '23 19:03

foxidrive