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.
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:
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.
- 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!!