Kirk Evans Blog

.NET From a Markup Perspective

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

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

Rate This
  • Comments 10

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


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.


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).


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:


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.


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:

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

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.

For More Information

Adding SharePoint 2010 PoweShell cmdlets to your PowerShell ISE


SharePoint 2010 Products administration by using Windows PowerShell

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

  • Thanks for useful info

  • Thanks, always knew there must be a better way and here it is.

  • To load any module upon opening PowerShell, do the following:

    1. Open PowerShell

    2. Type: $profile

    3. Create the path and file if required --OR-- for all PowerShell/ISE/USERS: create the file C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

    4. Open the .ps1 in PowerShellISE

    5. Modify to include modules you require, i.e.: Import-Module *applicablePSmodulehere*

    6. Save

    7. Test (close all PowerShells, open new one)

    8. Cake?

  • thanks for this, i found on my particular OS installation, that i had to copy the profile.ps1 form /examples folder.  alternatively, create an empty profile.ps1 file!

  • Thanks!  This is just what I needed.

  • It seems that at some point during all these CU's since your post.  if the machine is rebooted, the Forefront Identity Sync Service stops and can't be restarted.  I renamed my Profile.ps1 and was able to start it successfully.  Go Figure...

  • This is so helpful! Thanks Kirk!

  • And what do you do when you get the error "Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.SharePoint.PowerShell' is not installed on this computer' ?

  • Tom, if you are in the same position as me, you are likely missing the Microsoft.SharePoint.PowerShell folder in C:\Windows\Microsoft.NET\assembly\GAC_MSIL.  I have yet to find a way to install this, but I believe this folder missing is related to why we are unable to add the snapin.

    I am using Windows 10 with Powershell ISE

  • In order for the SharePoint bits to be available on the local computer, you need to have SharePoint installed on the local computer.  I believe that SharePoint 2010 was the last release that you could install on a client OS, and I am unsure if it was supported only on Windows 7.  You cannot install SharePoint 2013 or SharePoint 2016 on a client OS like Windows 8 or Windows 10, you must install it on a server OS.  You could instead use a script that initiates PowerShell remoting to another machine, enabling you to run PowerShell commands against a SharePoint server that you manage.  

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
Translate This Page