Visual Studio Command Prompt via PowerShell

 

Over the past couple of weeks I’ve been wanting to get into playing with some PowerShell for scripting and replacing the old trust cmd (the old DOS prompt is soo Win3.5 :p ).

 

It wasn't until I admitted this dirty little secret of mine to a friend and colleague Ben Nunney, that I really started to find out the real power of PowerShell (get it power). Ben took some time out to run through some of the cool features of PowerShell and how it can be used to contact remote machines and servers, both on premise and in the cloud.

 

However, one of the first things that I really wanted to do as a dev, was to use PowerShell to do everything that I would normally have to load up Visual Studio Command Prompt to do, after all who wants to be switching between prompts all the time.

 

With a lot little help from these blogs (link & link) I was able to create a method (VsVars32) that when executed would run PowerShell as VSCmd and just because I’m lazy I have added this to my PowerShell profile with a call to the method so that when PS launches it automatically loads into VSCmd mode.

 

The other addition that I have made is a check to see whether I’m running my shiny VS enabled PowerShell in admin mode or not, due to my continued aggravation if I fail to start VSCmd in non-admin mode and a local build wont run correctly. This method (AmIAdmin) will simply check if I'm running in admin mode, and if not display “Non-Administrator” to the title bar and turn the background a light blue colour so I can instantly see that I don’t have admin rights.

 

Here is the code that has been added to my PS profile (view using “notepad $profile”):

 

 

    1: #### Functions Used to Load VS Command Prompt #####
    2:  
    3: function Get-Batchfile ($file) {
    4:     $cmd = "`"$file`" & set"
    5:     cmd /c $cmd | Foreach-Object {
    6:         $p, $v = $_.split('=')
    7:         Set-Item -path env:$p -value $v
    8:     }
    9: }
   10:  
   11: function VsVars32()
   12: {
   13:     $version = "9.0"
   14:     $key = "HKLM:SOFTWARE\Microsoft\VisualStudio\" + $version
   15:     $VsKey = Get-ItemProperty $key
   16:     $VsInstallPath = [System.IO.Path]::GetDirectoryName($VsKey.InstallDir)
   17:     $VsToolsDir = [System.IO.Path]::GetDirectoryName($VsInstallPath)
   18:     $VsToolsDir = [System.IO.Path]::Combine($VsToolsDir, "Tools")
   19:     $vs90comntools = (Get-ChildItem env:VS90COMNTOOLS).Value    
   20:     $batchFile = [System.IO.Path]::Combine($vs90comntools, "vsvars32.bat")
   21:     Get-Batchfile $batchFile
   22:     [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
   23: }
   24:  
   25: ###### Functions Used to Load VS Command Prompt #####
   26:  
   27: ###### Function Used to Set Background to Light Blue If not Admin ######
   28:  
   29: function AmIAdmin()
   30: {
   31:   $wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
   32:   $prp=new-object System.Security.Principal.WindowsPrincipal($wid)
   33:   $adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
   34:   $IsAdmin=$prp.IsInRole($adm)
   35:   $title = [System.Console]::Title
   36:   if (!$IsAdmin)
   37:   { 
   38:     $title = $title + " (Non-Administrator)"
   39:     (Get-Host).UI.RawUI.Backgroundcolor="Blue"
   40:     cls
   41:   }
   42:   else
   43:   {
   44:     $title = $title + " (Administrator)"
   45:   }
   46:   [System.Console]::Title = $title
   47: }
   48:  
   49:  
   50: ###### Run Functions on StartuP ######
   51: VsVars32
   52: AmIAdmin

 

UPDATE

As I have been doing work with Windows Azure recently I have also added a handy way to access all the features of the Azure SDK Command Line Tool to my PowerShell by adding the following line to my profile:

$env:Path = $env:Path + "; C:\Program Files\Windows Azure SDK\v1.0\bin\"

This will add the folder containing all the Azure utilities (cspack, csrun, etc.) to the PowerShell environment.

 

Technorati Tags: Visual Studio,cmd,PowerShell,Visual Studio Command Prompt