Powershell and visual studio vcvars32.bat

As I had the same problem a few months back when first starting using PowerShell I thought I’d post my solution to it here, as I think it is more general. It can take any bat file’s environment and hook into PowerShel

And it will work with all version of Visual Studio (at least those you might still use)

First the function I can call from any shell (32 or 64 bit) to get the environment for the proper version of VS.

And then the ugly but functional envbat function which can take any bat file and "steal" its environment and add it to psh.

1:function envbat

2: {

3: $oldenv = @{}

4: $newenv = @{}

5: get-childitem env: | foreach-object { $oldenv[$_.Key] = $_.Value }

6:

7: $cmd, [string]$args = $args

8: cmd /c " `"$cmd`" $args > nul && set " | Foreach-Object {

9:if($_ -match"^(.*?)=(.*)$")

10: {

11: $newenv[$matches[1]] = $matches[2]

12: }

13: }

14:

15: $newenv.Keys |

16:foreach-object {

17:if (!$oldenv.ContainsKey("$_")) {

18: echo "Adding $_"

19: set-item -Path env:$_ -Value $newenv[$_]

20: } elseif ($oldenv[$_] -ne $newenv[$_]) {

21: echo "Changing $_"

22: set-item -Path env:$_ -Value $newenv[$_]

23: }

24: }

25:

26: $oldenv.Keys |

27:foreach-object {

28:if (!$newenv.ContainsKey("$_")) {

29: echo "Removing $_"

30: remove-item -Path env:$_

31: }

32: }

33: }

So there you have it. A quick explanation, it first remember the current psh environment, runs the batch file followed by "set" (line 8) which produces a list of all environment variables from the cmd shell. Walks through and stores those that match the regexp. The rest is just a matter of updating the psh environment with the cmd keys.