Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this post I’ll show a solution for automating tasks when opening a solution in Visual Studio.
In the grand scheme of things it hasn’t been that long since NuGet first arrived, but I can barely remember what I did without it! Aside from the standard benefits, one of the things I love about it is the Package Manager Console. Yes, I’m a keyboard fan and (having assigned a shortcut key for it) I can easily add packages without taking my hands of the keyboard. But the console also gives me PowerShell inside Visual Studio!
When running inside the console, you can invoke standard PowerShell cmdlets and aliases. For example, dir or Get-ChildItem will work (and you start in the solution directory which is convenient!). NuGet also adds additional cmdlets, e.g. Install-Package, Get-Project. Added to that, you get some extra context, so $dte will give you access to the top-level DTE instance for the Visual Studio automation API. So entering $dte.Solution.FullName will give you the full path to the currently open solution.
You probably know that NuGet packages can add references to assemblies and also add content files (handy for adding script files for web projects, or bootstrapper classes). NuGet packages can also run PowerShell scripts to help with the installation process, e.g. to add sections to a config file. The documentation has a section that outlines the scripts:
When I’m creating demos I try to be reasonably disciplined and do things like
I had a particular session a while back that I originally co-presented with a colleague, but subsequently ended up presenting it on my own. This was a bit of a challenge as some of the demos needed a few configuration steps after loading the solution to keep the flow of the session. When it was just me presenting there was no time to do that, so I pondered how to solve this. The solution? NuGet and PowerShell scripts!
The solution was actually quite simple: create a NuGet package with an Init.ps1 that runs when the solution loads and finds and executes a known script in the solution.
I’ve not done any thorough testing of the package, but it has worked for me . Feel free to give it a go and let me know how you get on!
To get started
When the solution is loaded, the _startup.ps1 will be executed, so any steps you want to happen automatically can be added here. Don’t forget that you have access to the DTE object to automate Visual Studio!
I’m not going to go into the implementation details in depth here as I think the concept is fairly simple once you know what the moving parts are. There are a couple of other things that I thought are worth mentioning.
Despite the usefulness of the automation, occasionally you don’t want the script to run. In these cases, hold down the shift key while the solution loads. When the package’s Init.ps1 script runs it checks whether the shift key is pressed before executing the _startup.ps1 script.
I found that there were a few common things that I wanted to do in my _startup.ps1 scripts, so I added them to the package as cmdlets. The DTE object model for Visual Studio is powerful, but not necessarily friendly
The Open-SolutionFile and Open-ProjectFile cmdlets both return an EnvDTE.Window object, so you can invoke methods on this, e.g.
(Open-ProjectFile "MyProject" "Foo\SomeFile.cs").Selection.GotoLine(10, $false) # params are lineno,select
Anonymous
April 02, 2014
Hey,
thanks for the article, some useful stuff there. I was just wondering if you had seen StudioShell? It does a lot of the heavy lifting for you in this respect. You can create a solution module that is invoked every time you open the solution in Visual Studio.
Gary
Anonymous
April 06, 2014
I can get EnvDTE.Window?! Great!
it makes me feel I don't need my visual studio addin project any more.
Crowdy
Anonymous
August 31, 2014
You are reinventing the wheel. Check out StudioShell for Visual Studio. Not only does it do the things you mention it provides a complete drive provider that presents the various hierarchies of the shell. Super cool project. Changed my entire perspective on the use cases for PowerShell.
Anonymous
October 07, 2014
Can you show the implementation of _startup.ps1 , kind of step by step approach, we are badly in-need of this approach,
Thanks in advance
Tony.
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in