How to execute PowerShell scripts and cmdlets in Visual Studio!
To get articles like this free in your inbox, subscribe to our newsletter.
As SharePoint developers, sometimes we need to do some IT Pro stuff such as creating a PowerShell Scripts for doing common tasks: deploying .WSP solutions to the farm, create some auxiliary lists, etc. In order to create and execute these scripts you can use the SharePoint 2013 Management Console or the Microsoft ISE (Integrated Shell Script Environment) as your scripting tools. However, most of the time we are using Visual Studio for our programming stuff and we don’t feel happy switching from Visual Studio to the scripting tool we have chosen…so the question around the corner is if there is a way to execute PowerShell Scripts in our favorite development tool.
Want to save money on SharePoint and have less overhead for general support and maintenance? Talk to an expert now!
Fortunately, the answer is “almost yes” (at the end of the post you’ll understand why the word “almost” appears in this statement) thanks to the PowerShell Tools for Visual Studio you can download from the Visual Studio Gallery:
http://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62-6eee37294597
In this article I will show you how to use these PowerShell Tools for executing PowerShell cmdlets in the IDE (Integrated Development Environment):
- Once you have downloaded and installed the PowerShell Tools in your CloudShare development environment, open Visual Studio 2013 (VS 2013) and create a new project in the IDE. In the “New Project” window you’ll find that there is a new “PowerShell Script Project” project template. Just create a new project using this template.
- As you will see in the Project Explorer, the project is composed only by a first empty PowerShell script. If you add some PowerShell sentences to the script and try to execute them, you will get some weird error messages indicating that it’s not possible to execute script in this environment. To fix this issue, just execute the Set-ExecutionPolicy cmdlet following the indications described in the following blog post: http://rostacik.net/2013/11/13/powershell-tools-for-visual-studio-2012-problem-ps1-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system-for-more-information-see-about_execution_policies/. If you find some additional problems when trying to execute this cmdlet, just select it in Visual Studio and press Ctrl + F8 so the cmdlet is effectively executed.
- Once you have fixed the execution scripts issue, you can execute any PowerShell script in Visual Studio, do some debugging adding breakpoints to the script, etc.
- For instance, if you have defined any variable in your script you have the possibility to inspect the content using the out of the box capabilities.
Want to save money on SharePoint and have less overhead for general support and maintenance? Talk to an expert now!
- Of course, in the Visual Studio output Window you can see the results of the script execution. Additionally, the Call Stack window allows you to see how the script is being executed by the IDE. If you are wondering if it is possible to execute SharePoint cmdlets in Visual Studio, the answer is “No for the moment”. Unfortunately, the PowerShell Tools for Visual Studio are not able to load the PowerShell SnapIn for SharePoint because is x64 based.
- Finally, you can verify that for any PowerShell command you want to use you have intelligence ready to help you in writing your PowerShell scripts in Visual Studio.
- For instance, if you execute the Get-Process cmdlet you’ll get in the Visual Studio Output Window all the process currently executing in your CloudShare environment.
And that’s all about how to execute PowerShell scripts in Visual Studio. Happy CloudSharing!!
If you found this article useful you might also be interested in:
How to install visual studio 2015
SharePoint content database size PowerShell
What you should do next…
1. Subscribe to our newsletter:
Subscribe to our newsletter below for the latest news, advice and thought-leadership for software professionals. Or visit our blog to browse our most recent articles.
2. Learn how virtual labs can grow your business:
To learn more about how CloudShare helps software organizations grow revenue, increase efficiency and improve quality, visit our resources page. You’ll be able to browser dozens of valuable white papers, eBooks, webinars, case studies, and brochures.
3. Get a FREE, no obligation demo:
Discover just how easy it is to create your cloud environment—in minutes! One of our friendly virtual labs experts will be happy to:
- Show you the platform in action
- Calculate pricing for your business
- Set you up with a 14-day free trial
- Answer any questions you have
- No pressure or obligation