Skip to content

Instantly share code, notes, and snippets.

@inh3
Created June 6, 2014 02:46
Show Gist options
  • Save inh3/29ee18f639bd7a7b3ec8 to your computer and use it in GitHub Desktop.
Save inh3/29ee18f639bd7a7b3ec8 to your computer and use it in GitHub Desktop.
Retain changes to environment variables set by a batch file when called from a powershell script
##############################################################################
##
## Invoke-CmdScript
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Invoke the specified batch file (and parameters), but also propagate any
environment variable changes back to the PowerShell environment that
called it.
.EXAMPLE
PS > type foo-that-sets-the-FOO-env-variable.cmd
@set FOO=%*
echo FOO set to %FOO%.
PS > $env:FOO
PS > Invoke-CmdScript "foo-that-sets-the-FOO-env-variable.cmd" Test
C:\Temp>echo FOO set to Test.
FOO set to Test.
PS > $env:FOO
Test
#>
param(
## The path to the script to run
[Parameter(Mandatory = $true)]
[string] $Path,
## The arguments to the script
[string] $ArgumentList
)
Set-StrictMode -Version 3
$tempFile = [IO.Path]::GetTempFileName()
## Store the output of cmd.exe. We also ask cmd.exe to output
## the environment table after the batch file completes
cmd /c " `"$Path`" $argumentList && set > `"$tempFile`" "
## Go through the environment variables in the temp file.
## For each of them, set the variable in our local environment.
Get-Content $tempFile | Foreach-Object {
if($_ -match "^(.*?)=(.*)$")
{
Set-Content "env:\$($matches[1])" $matches[2]
}
}
Remove-Item $tempFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment