Many of the scripts that I build end up being handed off to other groups for implementation or to run on a regular basis.  Because of this, any red on the screen from an error message usually ends up in a call.  In some cases where the script is something that is just run via a copy-and-paste into the shell, I’ll instruct the person running the script to run from the SharePoint 2010 Management Shell, but not everyone follows instructions, after all – PowerShell is PowerShell, right?

So then I went to including the following a the top of my scripts to make sure my scripts for SharePoint would run properly in PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell;

And that worked well, except of course when they were actually already running in the SharePoint 2010 Management Shell, in which case, more red text!  End result is another call.

image

So now I use the following code snippet at the top of my PS scripts:

if(-not(Get-PSSnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

And this way, if it’s run from regular PowerShell or the SharePoint 2010 Management Shell, no red text!

image

image

Which means no calls!