none
Adding SharePoint PowerShell snapins

    Question

  • Hello All, I am new to both working with PowerShell and Servers originally performing more frontend development. That said, I am now working with SharePoint Srever 2010 of which is installed on Server 2008 (non-R2 version).

    I have PowerShell installed however every time I want to run Backup, Restore commands I am alwasy having to add: Add-PSSnapin Microsoft.SharePoint.PowerShell.

    Is there a way to permanantly add this snap in so I don't have to add it every single time? is there a way to permanantly add any and all sanpins so I don't experience any unrecognizable cmdlets?

    Don't want to make others angry but I amnot seing what all the praise is about in PowerShell at the moment.

    • Moved by Mike Walsh FIN Thursday, March 25, 2010 4:10 AM 2010 questions go only to a suitable 2010 forum A suitable forum is not one with "pre-SharePoint 2010" in the Title of the forum. (From:SharePoint - General Question and Answers and Discussion (pre-SharePoint 2010))
    Wednesday, March 24, 2010 11:55 PM

Answers

  • Mark - you can set up your profile to automatically load the sharepoint snap-in regardless of the tool you use (the ISE for example) - here's how:

    • Loading the SharePoint snap-in via your profile script allows you to use any editor
    • 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
    
    • Add the following code to the script file and save your changes:
    $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"
    }
    


     


    Gary Lapointe, Blog: http://stsadm.blogspot.com/, Twitter: http://twitter.com/glapointe
    Saturday, April 10, 2010 3:17 AM
  • Another method that also works well is just to create a new shortcut with the following:

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit add-pssnapin microsoft.sharepoint.powershell

    Loads the SharePoint add on when started. Putting the shortcut into the all users desktop allows anyone logging in to use it, no profile setup needed.

    Thursday, August 12, 2010 1:10 PM
  • I would just add the line:

    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

    As the first line of the script that you are scheduling.

    If it's already there, no problem. If not, it adds it.

    Olddog

    Tuesday, March 27, 2012 3:12 PM
  • Are you opening the SharePoint Powershell application from the SharePoint program folder, or are you just opening the server's OOTB Powershell app?  The one in the SharePoint folder should already have the snap-in installed unless you're doing this from a server that doesn't have SharePoint installed.

    PS is way faster than STSADM and has more capabilities.


    SharePoint Architect || My Blog
    Thursday, March 25, 2010 12:06 AM

All replies

  • Are you opening the SharePoint Powershell application from the SharePoint program folder, or are you just opening the server's OOTB Powershell app?  The one in the SharePoint folder should already have the snap-in installed unless you're doing this from a server that doesn't have SharePoint installed.

    PS is way faster than STSADM and has more capabilities.


    SharePoint Architect || My Blog
    Thursday, March 25, 2010 12:06 AM
  • Hello Clayton thank you for the quick response,

    Even using the SharePoint 2010 Management Shell, I have still had to add the snapin. I have also used the PowerShell ISE having to add the same snapin. Any ideas?

    Thursday, March 25, 2010 12:30 AM
  • Maybe I would have the same issue if I was running backup/restore commands, but with all the commands I use, I don't have to change anything.
    SharePoint Architect || My Blog
    Thursday, March 25, 2010 1:08 AM
  • Thank you again Clayton, I am going to re-install the Management Shell and see what happens. Another question you might be able to answer....

    Thursday, March 25, 2010 1:38 AM
  • Was the next question...how to re-install the SharePoint 4.0 Management Console?
    SharePoint Architect || My Blog
    Thursday, March 25, 2010 1:52 AM
  • No it was not sorry I did not end the statement there and thank you for your feedback. I re-installed and it worked.
    Thursday, March 25, 2010 2:01 AM
  • Mark - you can set up your profile to automatically load the sharepoint snap-in regardless of the tool you use (the ISE for example) - here's how:

    • Loading the SharePoint snap-in via your profile script allows you to use any editor
    • 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
    
    • Add the following code to the script file and save your changes:
    $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"
    }
    


     


    Gary Lapointe, Blog: http://stsadm.blogspot.com/, Twitter: http://twitter.com/glapointe
    Saturday, April 10, 2010 3:17 AM
  • Another method that also works well is just to create a new shortcut with the following:

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit add-pssnapin microsoft.sharepoint.powershell

    Loads the SharePoint add on when started. Putting the shortcut into the all users desktop allows anyone logging in to use it, no profile setup needed.

    Thursday, August 12, 2010 1:10 PM
  • Keep in mind that the approach of editing shortcut like this does work but doesn't set the thread options which can cause issues in some scenarios. Preferred way is to edit the profile but if you're going to do what you've done then you're better off modifying it to load the sharepoint.ps1 script (that's all the management shell does) - this way you're sure the thread options are set.
    Gary Lapointe, Blog: http://stsadm.blogspot.com/, Twitter: http://twitter.com/glapointe
    Thursday, August 12, 2010 4:04 PM
  • I eventually did switch over to using the profile method. In the My Documents folder I created a folder named WindowsPowerShell. Inside of it I have a file named Profile.ps1. In Profile.ps1 I have the following:

    add-pssnapin microsoft.sharepoint.powershell

    This works great and I don't have to worry about if I'm using the shortcut or not.

    Wednesday, September 15, 2010 9:03 PM
  • I cannot agree more. What a hassle. What a bad information to kick you off. I just want to manage sharepoint, not stumble accross these PS and SP modules with explanations in all kind of blogs and fora. 

    Quote: Don't want to make others angry but I amnot seing what all the praise is about in PowerShell at the moment.


     

    Wednesday, January 26, 2011 1:17 PM
  • Ajj. I am debugging forum postings again to get PS started for SP :-( Is this really the way forward ?   

     

    Summary of the above :

    1) Shortcut (on (public) desktop 

    SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit add-pssnapin microsoft.sharepoint.powershell 

    Problem: doesn't set the thread options which can cause issues in some scenarios.

    Better: load the sharepoint.ps1 that is,  

    SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe sharepoint.ps1  


    2) Profile.ps1 method 

    add-pssnapin microsoft.sharepoint.powershell 

     

    Both to the same namely "add-pssnapin microsoft.sharepoint.powershell". What am I missing here ? 

     

    And isn't just the best to start from a shortcut on the (public) desktop:  

    SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe sharepoint.ps1  

    Only then I work as it is designed, namely the SP-Mngt.-Shell 

     

    Reference: 

    http://technet.microsoft.com/en-us/library/ee806878.aspx 

    The SharePoint 2010 Management Shell and the Windows PowerShell console also differ in the use of the ReuseThread option, which defines how the threading model is used. The management shell's use is defined by this line, {Host.Runspace.ThreadOptions = "ReuseThread"}, which is in the SharePoint.ps1 file. For more information, see PS Thread Options (http://go.microsoft.com/fwlink/?LinkId=183145).

     

    I still have the problem that scripts started by the TaskScheduler have no SP module loaded :-( 

    Wednesday, January 26, 2011 1:34 PM
  • I would just add the line:

    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

    As the first line of the script that you are scheduling.

    If it's already there, no problem. If not, it adds it.

    Olddog

    Tuesday, March 27, 2012 3:12 PM
  • I would just add the line:

    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

    As the first line of the script that you are scheduling.

    If it's already there, no problem. If not, it adds it.

    Olddog

    While this would add the snappin it could also "silentlycontinue" through a problem like not being installed.

    The solution GLP recommends works pretty much the same way you recommend while still catching real problems:

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }


    silly rabbit, tricks are for kids!

    Sunday, May 06, 2012 4:17 PM
  • I created this template for my sharepoint scripts based on David Kay's advise:

    # COMMENT: SharePoint Template for PowerShell and SharePoint 2010
    # 
    # ==============================================================================================
    If ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null){
    Import-Module WebAdministration
    }
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    $ver = $host | 
    select version 
    if ($ver.Version.Major -gt 1) {   
    $Host.Runspace.ThreadOptions = “ReuseThread” 
    }
    Start-SPAssignment -Global
    # insert new code here
    Stop-SpAssignment

    Thursday, May 31, 2012 9:35 PM
  • I know this is old but...

    "Welcome"
    
    "Loading modules..."
    Get-Module -ListAvailable | Import-Module
    
    "Loading snapins..."
    Get-PSSnapin -Registered | Add-PSSnapin
    

    
    
    

    David Jenkins

    Thursday, August 29, 2013 4:13 PM