Wednesday, May 9, 2012

Setup Powershell for .NET development

Here’s a quick recipe to setup a Powershell Command Prompt for development with Visual Studio and .NET in general.

Being a complete Powershell noob until quite recently I was not sure where to start. All the required info is out there but fragmented so I figured I’d put it here for other developers that run into the same problem.

What I want

I want a PowerShell box to do everything the Visual Studio Command Prompt can do and then some.

  • Run builds with MSBuild
  • Run Visual Studio tools and Windows SDK tools
  • Work with Git from the command line

How I got it working

PowerShell is easily extended using Modules. Fortunately, there’s a lot of those available already. Being a noob on Powershell I was not exactly sure how to install modules. Thankfully, there’s a module for that too.

Easy module installation with Psget
PsGet is a handy module installer named that supports a directory of modules. Installation of new modules is a no-brainer with a single command. Installation of PsGet (one time only) is a bit more complicated:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

The above command is copied verbatim from the PsGet home page.

Support for Visual Studio and MSBuild
Visual Studio comes with a batch file that configures the environment (path and variables) for .NET development. It’s the same batch file used by the Visual Studio Command Prompt. We can use that to configure PowerShell too. However, in PowerShell any changes to the environment by a script or batch file are not transferred to the calling environment. There’s a fix for that in the PowerShell Community eXtensions module (pscx). So we’ll use that:

Install-Module pscx

When PowerShell starts up it loads the user profile first so we can use that to configure the environment. The profile is located under

  %HOME%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

If the file doesn’t exist yet, create it and add the following lines:

Import-Module Pscx 

# Load Visual Studio environment Invoke-BatchFile 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat' x86

You may need to modify the path if you have a 32-bit Windows version or installed VS in a different location.

Now when you start a PowerShell prompt, you can run MSBuild and all the other tools used in .NET development.

Git support
If you’ve already installed MSysGit you should be able to use git from PowerShell. To further integrate git into PowerShell you can use posh-git. Posh-git provides command completion using the Tab-key and adds the repository status to the prompt. To install using PsGet:

Install-Module posh-git

The installation will update your PowerShell profile to load the module automatically.

References

No comments: