Expert Texture Home Contact me About Subscribe Digipede Connect on LinkedIn rwandering on Twitter rwandering on FriendFeed

rwandering.net

The blogged wandering of Robert W. Anderson

VS2005 PowerShell Prompt

If you are like me, you use the Visual Studio Command Prompt frequently, because you need the environment that gets set up by the vcvarsall batch. I’m using the PowerShell now exclusively, but still need this environment. Just calling the batch file doesn’t get the variables set in PowerShell. Adam Barr has a good explanation and solution here (and the interesting bit below is taken from his blog and credited there to Bruce Payette).

My goal was to have these environment variables set for every instance of PowerShell. Not rocket-science, but not terribly straight forward to a newbie PowerShell user either. To do that, I needed to add a script that will get run on every invocation of PowerShell. Here is what I did.

If you don’t yet have a profile set up, then create the required directory (explanation of profile naming is here):

mkdir "$home/My Documents/WindowsPowerShell"

Open your profile (or create if it doesn’t exist):

notepad "$home/My Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1"

Paste the following into your profile:

pushd 'C:\\Program Files\\Microsoft Visual Studio 8\\vc'
cmd /c “vcvarsall.bat&set” |
foreach {
  if ($_ -match “=”) {
    $v = $_.split(“=”); set-item -force -path "ENV:\\$($v[0])"  -value "$($v[1])"
  }
}
popd

And then start a new PowerShell. Now the environment should be set properly.

Update: I fixed the path to the default profile.  Thanks to dreamlusion for pointing out that my info was pre-release and out of date.

Tags: , , ,

    Trackback

5 Comments »

    Lee wrote @ May 12th, 2006 at 10:23 pm

Hi Robert;

That script has a slight bug in that any environment variables with ‘=’ in them will not be set correctly. Here’s another option, at the bottom of http://www.leeholmes.com/blog/NothingSolvesEverythingPowerShellAndOtherTechnologies.aspx

Lee

    Robert W. Anderson wrote @ May 13th, 2006 at 7:43 am

Lee — thanks for the comment — I’ve been reading your blog for some time, but I missed that detail. I suspect this works for the VS variables, but I’ll update the post soon.

Thanks, again.

[…] You may need a Visual Studio PowerShell Prompt to do […]

    dreamlusion wrote @ July 6th, 2008 at 6:38 am

Hi Robert,

Windows PowerShell profile locations have changed since this post was written:
http://www.google.com/search?q=windows+powershell+profiles+site%3Amicrosoft.com

So this “$home/My Documents/PSConfiguration/Microsoft.PowerShell_profile.ps1″ should be:
“$home/My Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1″

Just my two cents.

    Lior Elia wrote @ January 15th, 2009 at 9:41 am

There’s a little bug in the script.
The double backslash after “ENV:” causes all new variables to be prefixed with a backslash.
So you get for example “\VCINSTALLDIR=c:\…” instead of “VCINSTALLDIR=c:\…”
The string in that line should be instead:

"ENV:\$($v[0])"

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>