Scripting Visual Studio with Windows PowerShell

One of the really cool new things that shipped recently that I've been trying to learn more about is Windows Powershell. What's really great about it is that if your object supports COM or .NET, you can automate it just by firing up the command line.

To get an instance of Visual Studio running, I can do the following :

$dte = New-Object -comobject "VisualStudio.DTE"

(Note that if you have multiple versions installed you can specify VisualStudio.DTE .7.0 for VS 2002, VisualStudio.DTE .7.1 for VS 2003 or VisualStudio.DTE .8.0 for VS 2005.)

Since the DTE object was created via code and not by a user action, VS runs in silent mode (no user interface shown). I can show the main form with:

$dte.MainWindow.Visible = $true

For a useless example, if I want to close all the windows in the shell, I can do the following:

foreach ($w in $dte.Windows) { $w.Close() }

The cool thing about this really isn't that I can script Visual Studio (I could already do that from IronPython, VBScript or any other environment that can talk to COM objects), but that I can do it from the same command line that I use for other tasks in Windows.