Can Visual Studio 2012 be automated using cmdlets? Can Visual Studio 2012 be automated using cmdlets? powershell powershell

Can Visual Studio 2012 be automated using cmdlets?


You can automate Visual Studio 2012 with PowerShell. The Package Manager Console is actually a PowerShell host. From the Package Manager Console, the variable $dte is the COM automation object that you use to automate most of Visual Studio.

See http://www.wintellect.com/blogs/jrobbins/using-nuget-powershell-to-replace-missing-macros-in-dev-11 for more details and some examples.


I realize this is an old question, but for those still interested in this...

There's actually not that much code involved in writing your own cmdlet to acquire a COM binding of a DTE object for use in PowerShell scripts. See my answer on a related question for the source code of a working Get-DTE ... cmdlet. Once you have the DTE binding it's pretty much the same process as all the examples you've likely seen elsewhere.

Example:

PS C:\> Import-Module .\GetDTECmdlet.dll;PS C:\> $dte = Get-DTE | Select-Object -First 1;PS C:\> $dte = Get-DTE -ProcID 8547 | Select-Object -First 1;PS C:\> $dte = Get-DTE -FromAncestorProcs | Select-Object -First 1;PS C:\> $dte.ExecuteCommand('Help.About');PS C:\> [Runtime.InteropServices.Marshal]::ReleaseComObject($dte); | Out-Null;


The source code for the cmdlet is probably a bit overkill unless you were wanting access to the DTE from a pre/post-build step like I was. But, it should be sufficient to get you started.

And, if you really want to avoid digging into building your own cmdlet, you can see this answer on the same question which essentially wraps the C# code from the cmdlet in a PowerShell Add-Type -TypeDefinition @"..."@ command so that you don't need GetDTECmdlet.dll at all.