Advertisement
gnuwin

Invoke-CmdScript.ps1

Dec 20th, 2021
1,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Invokes a Cmd.exe shell script and updates the environment.
  2.  
  3. # Stolen from:
  4. # https://www.itprotoday.com/powershell/take-charge-environment-variables-powershell
  5.  
  6. # ORIGINAL URL (DEFUNCT):
  7. # http://windowsitpro.com/powershell/take-charge-environment-variables-powershell
  8.  
  9. # Proper credit given where credit is due. This is madness...
  10.  
  11. function Invoke-CmdScript {
  12.   param(
  13.     [String] $scriptName
  14.   )
  15.   $cmdLine = """$scriptName"" $args & set"
  16.   & $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
  17.   select-string '^([^=]*)=(.*)$' | foreach-object {
  18.     $varName = $_.Matches[0].Groups[1].Value
  19.     $varValue = $_.Matches[0].Groups[2].Value
  20.     set-item Env:$varName $varValue
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement