Ever since I first saw a demo of PowerShell some five years ago, I’ve been wanting to use it for development work; such as automating code generation or whatever other task for which small scripts are needed. It’s such a powerful environment, and I simply love the fact that it’s based on .NET and that you can easily create new Cmdlets! What’s been holding me back so far is that none of the tools I regularly use (SvcUtil, InstallUtil, etc.) are available in the default shell.
Sure, I could manually add the necessary directories to the shell’s path, but since there’s always been a Visual Studio Command Prompt (driven by vsvars32.bat), that seemed to me like unnecessary duplication of effort. Luckily, Chris Tavares has a solution! …or, that’s at least what I thought when I first read his post.
Unfortunately, it didn’t work for me. While I’m sure that Chris tested his solution, my registry simply doesn’t contain the InstallDir node that he uses. Whether that’s because I’m running on Vista x64 (which causes many funky issues from time to time), or my Visual Studio installation is just plain weird, I don’t know… At least, Chris’ post solved the most difficult problem for me: Getting the vsvars32.bat path and other environment variables into PowerShell.
On every machine that I’ve ever had Visual Studio installed, there’s always been an environment variable called VS90COMNTOOLS that points to exactly the folder containing vsvars32.bat, so why not use that instead of the registry approach?
While I kept Chris’ Get-BatchFile function unmodified, I reimplemented the VsVars32 function as:
$vs90comntools = (Get-ChildItem env:VS90COMNTOOLS).Value
$batchFile = [System.IO.Path]::Combine($vs90comntools, “vsvars32.bat”)
[System.Console]::Title = “Visual Studio 2008 Windows PowerShell”
As you can see, getting the correct path to vsvars32.bat is as simple as extracting the VS90COMNTOOLS environment variable and then using Path.Combine to construct the full path to the file.
To get a Visual Studio 2008 PowerShell (as an equivalent to the Visual Studio 2008 Command Prompt), I combined it all in a vsvars32.ps1 file. To run the script file and keep the shell open, you need to open it like this, since double-clicking a .ps1 file will just open it in Notepad:
powershell.exe -noexit <path>\vsvars32.ps1
where <path> indicates the path to the vsvars32.ps1 file. To make it easier to quickly open a Visual Studio 2008 PowerShell, I created a shortcut with this command, and added the shortcut to my Start menu. Now I can simply type Visual Power in my Vista Start menu search box, and I then have instant access to my Visual Studio 2008 PowerShell.
As a convenience to you, my dear reader, I’m attaching the script file for your reuse – use at your own peril.
If you are new to PowerShell, note that you must change your execution policy to RemoteSigned or less (or sign my script with a certificate you trust) to be able to run scripts at all, and that you probably also need to go to the file’s properties and unblock it, since you downloaded it from the internet.
Update: You can also move the VsVars32 (and corresponding Get-BatchFile) function into your profile.ps1, and just call VsVars32 from your vsvars32.ps1 file. That enables you to use VsVars32 from other scripts without having to explicitly open a Visual Studio 2008 PowerShell.