Q:

Write values from a powershell file into variables of the wrapping batch file

MyBatScript.bat:
PowerShell.exe "call MyPowershellScript.ps1"
%Tag% = NotSet
%CommitId% = NotSet

MyPowershellScript.ps1:
REM override the NotSet default value of tag and commitId defined in the MyBatScript.bat
$longVersion = git describe --long
$versionTokenList = $longVersion.Split('-')
%Tag% = $versionTokenList[0]
%CommitId% = $versionTokenList[-1]

I want to write the Tag and CommitId value from the powershell script called by the wrapping batch file into 2 batch variables.
How can I do that? At the moment the code above works not...
NOTE: I know you guys will ask my why I mix old bat with powershell.
I did not do that... its the mess of someone else I just have to fix the crap and I really need to do it this way... Thanks for your patience and understanding.

A:

%varname% is batch syntax for variables. It's not valid in a PowerShell script. The canonical way to return values from a PowerShell script to a parent batch script is to echo the values in the PowerShell script (for instance as a semicolon-separated string):
$longVersion = git describe --long
'{0};{1}' -f $longVersion.Split('-')[0,-1]

and process the output with a loop in the batch script:
@echo off

for /f "tokens=1,2 delims=;" %%a in (
  'PowerShell.exe -File "MyPowershellScript.ps1"'
) do (
  set "Tag=%%a"
  set "CommitId=%%b"
)

