4

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.

Amal G Jose
  • 2,486
  • 1
  • 20
  • 35
vareda
  • 125
  • 2
  • 10
  • See if this post is what you are after. http://stackoverflow.com/questions/3414300/calling-powershell-from-batch-and-retrieving-the-new-value-of-a-temporary-envir – malexander Dec 04 '13 at 05:10
  • @malexander Thank you for the link. But in this they are getting output of a powershell command say `get-date` and reading the value in batch script. – vareda Dec 04 '13 at 05:27
  • Also, tried to escape the closing parenthesis as below but still getting error. `FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"; (Get-Variable myname^).value') DO SET VAL=%%i echo %VAL%` – vareda Dec 04 '13 at 05:31
  • Correct me if I'm wrong, but doesn't BAT script recognize \ as an escape character? – Vish Dec 04 '13 at 05:56
  • @Vish According to my understanding \ will be used as escape character for regex patterns only. Please check [here](http://www.robvanderwoude.com/escapechars.php) – vareda Dec 04 '13 at 06:05
  • Yes, correct - I ran your code and got the error irrespective of using the \ escape character. Thanks for pointing that out. – Vish Dec 04 '13 at 06:10
  • I have solved it!! I have to escape not only closing parenthesis and also the semi colon. I got a different error after adding escape character for closing parenthesis. `SET VAL=Get-Variable : Cannot find a variable with name 'myname'.` Working command...`FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"^; (Get-Variable myname^).value') DO SET VAL=%%i` – vareda Dec 04 '13 at 06:11
  • Interesting. I thought I almost got it - in commandline at least - but then it failed in a BAT script. One reason why I ported all my BAT scripts over to Powershell. Totally worth the effort! – Vish Dec 04 '13 at 06:23
  • How can we handle if multiple values returned/exported from a powershell and save it in variables in batch file – VSanka Oct 07 '20 at 12:37

2 Answers2

7

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Hi @foxidrive, can u let me know if we can handle multiple values returned/exported from a powershell and save it in multiple variables in a batch file – VSanka Oct 07 '20 at 12:38
0

Moved the answer added yesterday from comments section.

I have solved it!! I have to escape not only closing parenthesis and also the semi colon. I got a different error after adding escape character for closing parenthesis.

SET VAL=Get-Variable : Cannot find a variable with name 'myname'. 

Working command is as follows...

FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"^; (Get-Variable myname^).value') DO SET VAL=%%i

@foxidrive Thank you for your answer and it also worked.

vareda
  • 125
  • 2
  • 10