Getting Powershell variable value in batch script

batch-file, cmd, powershell, windows

Solution

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

Problem

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.

Original source

Related problems