Comments
 
posted on June 15th 2015, at 11:54
by lunarg
When running multiple scripts in a session, which use and add the same snap-in using Add-PSSnapin, only the first one succeeds. Subsequent attempts to add the same snap-in will result in an error:

Error
Cannot add Windows PowerShell snap-in My.SnapIn because it is already added. Verify the name of the snap-in and try again.

You can resolve this issue by enclosing it in the following if-statement:

if ( (Get-PSSnapin -Name My.SnapIn -ErrorAction SilentlyContinue) -eq $null ){ Add-PsSnapin My.SnapIn}

It (silently) checks the presence of the requested snap-in. If it does not exist (i.e. the check returns $null, then it loads the snap-in.

Note: replace My.SnapIn with whatever snap-in you   ...