Powershell 错误添加 PSSnapin "Microsoft.SharePoint.PowerShell"

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

当我使用管理员权限 运行 下面的 Sharepoint 2010 管理 shell 命令时出现错误。

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

检查模块是否已经加载:

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

来源:

更新:

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

}