13

I'm attempting to use the PowerShell ISE on a Win 2012 Standard Server.

I followed the steps here (http://sharepointpromag.com/blog/powershell-and-sharepoint-step-1-loading-dlls-and-snap-ins) to add the SharePoint Snapin which worked on my DEV box but in my TEST environment I'm having some strange behavior.

This lists all of the assemblies without any issue.

[AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { Split-Path $_.Location -Leaf } | Sort

I load Microsoft.SharePoint.PowerShell with this assembly and verify it with the command above. However, I have to explicitly load each DLL. If I use "Microsoft.SharePoint" as the value it does not load the PowerShell assembly.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.PowerShell")

This does not display any values, SharePoint or otherwise which if found the strangest part.

Get-PSSnapin

This fails which makes sense, since there are not Snapins listed about.

Add-PSSnapin Microsoft.SharePoint.PowerShell

On another note, I'm able to execute the SharePoint Powershell console without an issue.

Question:
How can I add the SharePoint Snapin in the PowerShell ISE?
How can I debug why no PSSnapins appear to be registered?

1
  • What version of SharePoint and what version of PowerShell is in use?
    – user6024
    Jan 1, 2014 at 1:06

2 Answers 2

14

Before you can do anything with PowerShell ISE, you need to make sure that (1) You are running PowerShell ISE as an Administrator and (2) that the execution policy is set in such a way that you can execute scripts.

Open SharePoint Management Shell and run

Get-ExecutionPolicy

If you get RemoteSigned, Unrestricted or Bypass you can safely continue. RemoteSigned means that you can't run scripts that are downloaded from the another server or the internet unless it is signed. If you download files to run in PowerShell ISE, set the execution policy to unrestricted, like this:

Set-ExecutionPolicy Unrestricted

If all works as expected, run PowerShell ISE as an Administrator. Then try to load the SharePoint.PowerShell with the following:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

When that has loaded, try another command, such as

Get-SPManagedAccount

If you receive no error messages, then you have SharePoint.PowerShell loaded. If not, run

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

and read the error messages. Copy the first line of the error received and try to search for it. Still in trouble? Update your answer with the error message, and I'll try to help if you notify me in a comment.

6
  • 1
    Thanks Benny , your solution works for me. You saved my life ;) Aug 6, 2015 at 3:36
  • Setting the execution policy to unrestricted is way too open for what you are doing Oct 28, 2016 at 16:53
  • @KolobCanyon when done one reset the execution policy back to its original state
    – Benny Skogberg
    Oct 28, 2016 at 17:54
  • Isn't Windows.SharePoint.PowerShell signed though? Oct 28, 2016 at 18:02
  • @KolobCanyon Defult setting for any installed Windows Server is complete lockdown - thus one have to enable/activate features to use
    – Benny Skogberg
    Oct 28, 2016 at 18:06
1

It seems like, from your description, if you ran this:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

it would have failed, because Add-PSSnapin "Microsoft.SharePoint.PowerShell" by itself failed. So I don't know why that other answer was marked as the answer, unless it was because of your execution policy. But the code above (the only other difference between what you did and that other answer) does not help you load Microsoft.SharePoint.PowerShell if you could not load it through just Add-PSSnapin Microsoft.SharePoint.PowerShell, alone.

You would have to run your script on a SharePoint server, where that DLL exists in the GAC. Or load it into the GAC on the workstation where you are running the script, presumably. To load it into Powershell after doing that, you are correct in running this:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.PowerShell")

and then seeing if it has been loaded with:

[AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { Split-Path $_.Location -Leaf }

The one thing you forgot was getting the DLL into the GAC (C:\Windows\assembly), to begin with, it sounds like, to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.