Tuesday, February 8, 2011

Adding the SharePoint Snap In to PowerShell

What's the first step? If you are using the ISE, and you want to have the scripts excited from any PowerShell environment:
Add-PSSnapin Microsoft.SharePoint.PowerShell

This line adds the SharePoint snapin and makes any PowerShell console the same as the SharePoint Management Console.

If you want to get cute you can add the following code to see if the SharePoint snapin is available, and throw a friendly error message if it is not:

$snapin = Get-PSSnapin | Where-Object{$_.Name -eq 'Microsoft.SharePoint.PowerShell'}
if($snapin -eq $null)
{
Write-Output('SharePoint snapin does not exist on this server')
}else{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}


As long as you know the namespace of the PowerShell snap in that you want to add, the above code will work to add it simply replace the SharePoint snapin namespace (Microsoft.SharePoint.PowerShell) with the namespace of your snap in.

No comments:

Post a Comment