Wednesday, January 7, 2015

Object Reference error when SharePoint PowerShell Snapin is added

In a SharePoint 2010 farm I wanted to use PowerShell Cmdlets, so run SharePoint 2010 Management Shell as administrator but faced below error:


Add-PSSnapin : Object reference not set to an instance of an object. At D:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\SharePoint.ps1:3 char:13 + Add-PsSnapin <<<< Microsoft.SharePoint.PowerShell + CategoryInfo : NotSpecified: (:) [Add-PSSnapin], NullReferenceException + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.AddPSSnapinCommand



Error I thought it will be a common problem, so googled it but found nothing! No one faced the same exact problem and similar error's solution didn't help me at all. what I tried :



  • Open Windows PowerShell and add SharePoint PowerShell Snapin manually, same error.

  • Check server Event viewer and SharePoint LOGS, everything was Ok.

  • Copy and replace SharePoint Assemblies from another healthy server to test if DLLs are corrupted or not, no success.

  • Re-run SharePoint Configuration Wizard hoping for getting another error to use as keyword for search, but wizard completed successfully!




I changed my way and searched for different methods of adding SharePoint PowerShell Snapin. I noticed that in some PowerShell scripts Microsoft SharePoint assembly was loaded before adding snapin. I don't know why but that Trick worked for me too! If you have same problem follow these steps: navigate to below directory:


%COMMONPROGRAMFILES%\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\



find SharePoint.ps1 file in this directory. this file is executed every time you run SharePoint 2010 Management Shell.


open it with a Text Editor software like Notepad (much easier than enabling and using Windows PowerShell ISE feature!). add below code before the line which SharePoint PowerShell Snapin has been added:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")|Out-Null



Load DLL


save the file and run SharePoint 2010 Management Shell to see effect of loading DLL !



Error Gone


Original Post (my first and last blog post so far!)




by Mehdi Boroujerdi via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment