Add Microsoft.SharePoint.PowerShell Snap-In to All PowerShell Windows

This post will show you how to ensure Add-PSSnapin "Microsoft.SharePoint.PowerShell" has been executed in all PowerShell windows.

Background

I have been doing a lot of work in PowerShell lately, and found myself frequently in the PowerShell ISE window writing scripts.  I was constantly adding the line Add-PSSnapin "Microsoft.SharePoint.PowerShell" at the top of every script I wrote, and I wanted it to just already be there like it is when I run the SharePoint 2010 Management Shell.  You can add the SharePoint snap-in to all PowerShell windows, so it will be available when you run PowerShell ISE and you don't have to remember to add the snap-in to every script you run.

Implementation

Credit goes to SharePoint and PowerShell guru Gary Lapointe for this tip.  Open up PowerShell ISE and run the following to create a profile script if one doesn’t exist and edit it in the ISE:

 if (!(test-path $profile.AllUsersAllHosts)) {new-item -type file -path $profile.AllUsersAllHosts –force}
powershell_ise $profile.AllUsersAllHosts

That will open a new tab in PowerShell ISE allowing you to edit profile.ps1.  If you simply execute $profile.AllUsersAllHosts, you will see the path where this file is stored (be default it is C:\Windows\System32\WindowsPowerShell\v1.0).

image

In PowerShell ISE, you will now have a new tab where you can edit this file.  In that new tab, add the following code and then save the file.

 $ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

The edit window looks like this:

image

Now, close PowerShell ISE and then open it again.  It will take a little longer than usual to open the window because it is executing the code from Profile.ps1 and adding the Microsoft.SharePoint.PowerShell snap-in.  To test it, run a command such as Get-SPFarm.

image

That simple little trick has saved me hours of frustration while editing scripts, and has enabled me to really start leveraging PowerShell ISE.  Thanks to Gary for his great insight at: https://social.technet.microsoft.com/Forums/en/sharepoint2010setup/thread/fcb77654-0f13-42e0-b181-6e52242fe9d6.

If you are interested in PowerShell and SharePoint and haven’t visited Gary’s blog, do so immediately… it is a great resource.  https://blog.falchionconsulting.com

Update: Spence Harbar let me know that he has a post doing something very similar, executing the same script that the SharePoint 2010 Management Shell executes.  https://www.harbar.net/archive/2010/05/03/adding-sharepoint-2010-poweshell-cmdlets-to-your-powershell-ise.aspx

For More Information

https://social.technet.microsoft.com/Forums/en/sharepoint2010setup/thread/fcb77654-0f13-42e0-b181-6e52242fe9d6

Adding SharePoint 2010 PoweShell cmdlets to your PowerShell ISE

Add-PSSnapIn

SharePoint 2010 Products administration by using Windows PowerShell

Add-PSSnapIn Microsoft.SharePoint.PowerShell Failing after SharePoint Install