Visual Studio vs. the PowerShell Command Line

Numerous times in the last month I’ve been working with different teams and when I whip out my PowerShell window and start doing all the magic, especially with Visual Studio command line tools, the young kids go crazy. The mix of command line development tools and PowerShell is a powerful aphrodisiac. Too bad that Visual Studio is living in the dark ages of batch files required to set various environment variables so everything works. The trick is getting those environment variables into PowerShell, which is not as easy as it should be.

For command line work, there’s two key batch files from Visual Studio that do the environment variables dance. The first is <VS Install Dir>VCVCVARSALL.BAT, which does the work to set up the PATH, LIB, INCLUDE, and other key environment variables. This is the batch file run when opening the Visual Studio Developer Command prompt and the other flavors of command prompts for ARM, cross compilers, etc. The second batch file is <VS Install Dir>Team ToolsPerformance Tools[x64]VSPerfCLREnv.cmd that you use to do command line profiling and code coverage, which again involves setting the PATH but also the key profiling environment variables that start with COR_*.

What we need is a seamless way to execute batch files so any environment variables that are added or removed automatically reflected in the PowerShell window. Numerous command line aficionados have implemented solutions to these issues, but I found that none of them take into account REMOVING environment variables, which is particularly important in the VSPerfCLREnv.cmd case.

You can get my solutions to the batch file problem in my WintellectPowerShell module up on GitHub. I implemented two cmdlets that you aspiring command line people will find helpful. If you want to Visual Studio tools all set up, execute the Import-VisuaStudioEnvironment cmdlet. It will look for the latest version of Visual Studio on your machine and call the VSVARSALL.BAT for that version and the current architecture. If you need to use a different version of Visual Studio that you have installed your computer, you can pass in the VS version and architecture as shown below. Look at the help for Import-VisualStudioEnvironment for all the appropriate values you can pass in. I’ve got this cmdlet in my profile after importing the WintellectPowerShell module so all PowerShell windows are automatically set up for command line development tool goodness.

  1. Import-VisualStudioEnvironment -VSVersion 2010 -Architecture x86

The work horse cmdlet is Invoke-CmdScript, which will run any batch file and bring the environment variables set into PowerShell. My version is based of the original version by Lee Holmes. What’s nice about my Invoke-CmdScript is it also handles removing environment variables the batch file executed clears. So, if you want to do command line profiling with trace and garbage collection analysis turned on, you can run the following and have everything ready to go in your PowerShell window.

  1. Invoke-CmdScript VSPerfCLREnv.cmd /tracegc

With these two cmdlets wrapping up the pain of batch files in PowerShell you should be read to let your command line freak flag fly as you code your way to nirvana.

We deliver solutions that accelerate the value of Azure.

Ready to experience the full power of Microsoft Azure?

Start Today

Blog Home

Stay Connected

Upcoming Events

All Events