-1

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
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
user990897
  • 177
  • 2
  • 6
  • 21
  • 1
    The SharePoint management shell already loads that plugin on startup. What problem are you trying to solve by loading it again? – Ansgar Wiechers May 05 '15 at 22:42
  • 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 . At line:1 char:13 + add-PsSnapin <<<< Microsoft.SharePoint.PowerShell + CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShel l:String) [Add-PSSnapin], PSArgumentException + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad dPSSnapinCommand – user990897 May 07 '15 at 13:08
  • getting error in below function error details:The term 'Reset-SPContext' is not recognized as the name of a cmdlet Function CreateSiteCollections([xml]$xmlinput) {$CreateApplicable = $false Write-Host -ForegroundColor White " - Creating Site Collections..." If ($xmlinput.Configuration.WebApplications) {ForEach ($webApp in $xmlinput.Configuration.WebApplications.WebApplication){if ($webApp.SiteCollections){Reset-SPContext #only reset if we are deploying sites.} – user990897 May 07 '15 at 19:04
  • I have no idea what that code snippet is supposed to tell us. Or why you didn't put it in your actual question. – Ansgar Wiechers May 07 '15 at 19:07
  • i think this is the issue with powershell 2.o which does not contain cmdlet. do you know why this error comes – user990897 May 07 '15 at 19:08
  • I suspect `Reset-SPContext` isn't part of anything native to SharePoint or PowerShell. This really doesn't seem relevant to the current question and should probably be a separate one. Likely sources of the cmdlet in question [here](http://www.codefornuts.com/2009/10/forcing-sharepoint-tool-to-reload-12.html) and/or [here](https://social.msdn.microsoft.com/Forums/office/en-US/5ea46e07-a4b0-4bab-b698-9b6eeb695607/sitedefinitions-installed-within-the-same-powershell-session-not-available-upon-site-collection?forum=sharepointgeneralprevious). – Booga Roo May 08 '15 at 01:56

1 Answers1

3

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{

}
Community
  • 1
  • 1
Marc Kellerman
  • 466
  • 3
  • 10
  • Slight wording concern about `-ListAvailable` "ListAvailable does not return information about modules that are not found in the PSModulePath environment variable, even if those modules are loaded in the current session." Probably not an issue for the SharePoint snap-in, but could affect others who are loading modules from custom or remote sources. [More details from the Get-Module help.](https://technet.microsoft.com/en-us/library/hh849700(v=wps.620).aspx) – Booga Roo May 05 '15 at 23:49
  • Excellent. I do prefer that second option. It is more specific to the snap-in vs module situation in question and is a more thorough solution. – Booga Roo May 06 '15 at 00:14
  • Getting below error 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 . At line:1 char:13 + add-PsSnapin <<<< Microsoft.SharePoint.PowerShell + CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShel l:String) [Add-PSSnapin], PSArgumentException + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad dPSSnapinCommand – user990897 May 07 '15 at 13:09
  • 1
    Snapins are not the same as modules. This is a much better answer http://stackoverflow.com/a/1478221/299327. – Ryan Gates Jul 27 '15 at 14:39