Search code examples
powershellsharepointsharepoint-2010powershell-2.0

Powershell error Add-PSSnapin "Microsoft.SharePoint.PowerShell"


I am getting an error when I run below command from sharepoint 2010 managment shell using admin rights.

Add-PSSnapin Microsoft.Sharepoint.Powershell

Add-PSSnapin : Cannot add Windows PowerShell snap-in Microsoft.Sharepoint.Power
shell because it is already added. Verify the name of the snap-in and try again

Solution

  • Check to see if module is already loaded:

    if (Get-Module -ListAvailable -Name SomeModule) {
      Write-Host "Module exists"
    } else {
      Write-Host "Module does not exist"
    }
    

    Source: How do I check if a powershell module is installed?

    UPDATED:

    Try { 
        if((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null){ 
            Add-PsSnapin Microsoft.SharePoint.PowerShell 
        } 
    }Catch{
    
    }