During my time of providing SharePoint support, I’ve experienced a number of issues that required PowerShell to resolve. These were situations that for one reason or another, the user was unable to resolve the issue through the Graphic User Interface (GUI). That is, without performing some form of restore from a backup, PowerShell was the only answer to actually fix the problem.
Scenario 1: A solution that requires SharePoint Server is activated on a SharePoint Foundation site.
In this case, the solution broke the client’s site since it required functionality that doesn’t exist in SharePoint Foundation. After the solution was activated, they received a very descriptive “An unexpected error has occurred” message when accessing the site.
This scenario had a relatively simple solution (no pun intended). First, it was necessary to identify the offending solution. The following can be used to list all of the solutions in a site collection:
Get-SPUserSolution -site http://sitecollection
After I had the name for the solution, I was able to deactivate it with the following:
uninstall-SPUserSolution -Identity solution.name.wsp -site http://sitecollection
Scenario 2: An item in a SharePoint list becomes corrupted.
In this case, the list item did not have all of the fields that other items in the list had. It also had the unpleasant effect of locking up the browser window any time the item would be displayed on a page.
I attempted a number of things through the GUI including creating different views in an attempt to find a way to access the list item and delete it. All of the methods through the GUI were unsuccessful. Since interacting with the list through the GUI became virtually impossible, it was necessary to turn to PowerShell to exterminate the offending item.
Here is the script I used to delete the corrupted item:
$w = get-SPWeb -Identity http://server_name
$l = $w.GetList("/Lists/somelist/")
$listItem = $l.GetItemById(int) #replace “int” with the ID of the desired item
Here is the lowdown on the script above:
- Lines 1-2 drill down into the SPWeb object to assign the list to the $l variable
- Line 3 assigns the corrupted list item to the $listItem variable
a. In this scenario, I was able to identify the corrupted list item by its ID, so I was able to get the corrupted list item with the getItemByID method.
- Line 4 deletes the selected item
- With Line 5, I didn’t want to forget to dispose the SPWeb object now that I’m finished with it!
The client’s list was usable again after getting rid of the corrupted list item.
Scenario 3: A site admin deletes the root site of a site collection.
For example, on the root site of a site collection, the admin does this:
I’ve run into this situation a number of times. Whenever you delete a sub-site of a site collection, it’ll go to the site collection recycle bin. But what happens when deleting the root site of a site collection? It effectively deletes the entire site collection.
“My site collection was here a minute ago…”
“But now it is gone!”
There is not a way to recover this through the GUI, but thankfully, starting in SharePoint 2010 Service Pack 1, there is an easy method using PowerShell to recover a site collection that was deleted this way. Here is how to do it:
First, it is necessary to get a list of the deleted site collections in the farm.
Next, I’ll use the GUID of the SiteId to run the restore command for the desired site collection:
Restore-SPDeletedSite -Identity 866e583e-2c00-4518-a8c2-4132b87c431f
Now the test site collection has been restored:
So in recap, whenever you’ve run into an issue with SharePoint that you can’t resolve through the GUI, don’t be afraid to investigate a solution through PowerShell. It can often provide a simple answer!