​​​Top PowerShell Commands Every SharePoint Admin Should Know

By Michael Ross

 



 

Top PowerShell Commands 

Every SharePoint Administrator will eventually run into issues that not only cause frustration, but potentially require massive amounts of time spent piling through tedious code.  Luckily there are PowerShell commands that can ease frustration and make you the hero-of-the-day.

First, a disclaimer:

Using PowerShell may cause dizziness and debilitating migraine headaches. Prolonged exposure to PowerShell may cause drowsiness and male pattered baldness, hives and nearsightedness. Please refrain from driving and operating heavy machinery while using PowerShell and make sure you have backups before running untested PowerShell commands.

(Ok...so only that last line is actually serious...)

1) Finding a Correlation ID error:

 


One frustrating error SharePoint users will occasionally experience is the dreaded 'red X' with a cryptic correlation ID error. When that happens, the SharePoint Administrator must perform the equally frustrating task of looking through the logs to find the cause of the error.


 

 

Correlation ID Error


 

Wouldn’t it be nice if there was a command that will search the log files for the correlation ID?  The command Merge-SPlogfile will automatically search the logs for the error ID. Better than that, the command will search ALL the servers in the farm and place a log file on the server that you will the command.  Here is an example of the the Merge-SPlogfile command:

Merge-SPLogFile -Path -Correlation <Guid>
(I typically run the command like this using the Correlation ID from sample error above.)

Merge-SPlogfile –Path c:\log.log –Correlation ba05e237-0680-403a-b9f6-e49f96ac55d4
(That will place a .log file on the C drive of the server which will only contain errors with the correlation ID.)

2) Quickly restore a deleted site collection.


 

One day I received a call from a frantic customer who accidentally deleted a very valuable site and needed it restored ASAP. I was still new, so their panic started to make me panic - especially when I heard that the deleted site had also been emptied from the Recycle Bin and the process to restore a site from SQL takes more time than we had.

Luckily, one of my team members was watching over my shoulder and said, “Use PowerShell to find the site and then restore it - it shouldn't even take 10 minutes."

Brilliant! Here are the PowerShell commands to restore a deleted SharePoint site:
Get-SPDeletedSite | select Path , siteid
(This will generate a list of all deleted sites and their GUIDs as seen below) 
      SharePoint SiteId

Then, run the command:
      Restore-SPDeletedSite -Identity (SITE ID)

(in this case, Restore-SpDeletedSite –Identity 4bff0c0e-4d56-4fec-8174-a60a16daeaac)

This final command will restore the site

3) Quickly check for errors on a content database

One day I received an assignment to migrate a content database from SharePoint 2010 to SharePoint 2013. This should have been a piece of cake...but 3 hours later I realized that my cake was stale! The upgrade had simply failed and I did not know why. Eventually I found a command that will check the content database - quickly solving the issue and adding milk to my cake (it's good, try it):


 

Test-SPContentDatabase -name WSS_Content_DB –webapplication
When I ran the test it revealed that one of the lists was blocking the upgrade:

Category: WideListSchema

Error: True
Upgrade Blocking: True
Message: The list 'Bigfoot Shoes ' at URL '/boggieman/Lists/'Bigfoot Shoes’ has a schema that is incompatible with this version of SharePoint.
Remedy: Delete some fields from the list 'Bigfoot Shoes '."

 

I noticed that the UpgradeBlock in was True and there was an issue with the list 'Bigfoot Shoes'.  That list had 10,000 items and 259 ROWS! Once I deleted that list, with the customers’ permission, the upgrade to SharePoint 2013 was successful.

 

I hope you find the above PowerShell commands helpful.  Please make sure you check the TechNet links provided to make sure the commands above will work on your system.

 

Get-SPDeletedSite - 
http://technet.microsoft.com/en-us/library/hh286316.aspx

Test-SPContentDatabase -
http://technet.microsoft.com/en-us/library/ff607941.aspx

Merge-SPLogFile -
http://technet.microsoft.com/en-us/library/ff607721.aspx


 

Mike Ross

SharePoint Architect

Mike.Ross@Rackspace.com

 

Powered by SharePoint 2013 © 2016 Rackspace, US Inc.