SharePoint 2010 – List all Sites where a feature is activated with PowerShell

Quick note because it seems like a lot of people are looking for this. I was dealing with a badly behaving feature but had to find all the places it was installed on a fairly ornate farm. Here’s what I finally came up with:

Get-SPSite -limit ALL | Get-SPWeb -limit ALL | foreach{ $_.features[“GUID”] } | select Parent

This will give you the site names where the feature is installed. The GUID is the ID of the feature. It could use some more work, specifically returning status and URL, but this will get you 90% of the way there.

Rename SharePoint Site Collections

Resuming my renaming spree, we’re going to take a look at how to rename SharePoint site collections. Our first stop is working with Site Collections. Site collections don’t have a name per se, but they do have a URL, and it’s something that folks often want to change. As of SharePoint 2010, there isn’t a direct way to change the URL for a site collection, so the accepted method is to back up the site collection, delete the existing collection, then restore the collection with the new URL. Kind of a pain, but better than starting from scratch…

Backup-SPSite URL –Path path [-Force] [-NoSiteLock] [-UseSqlSnapshot]

Force: Overwrite the backup file if it already exists

NoSiteLock: Forces the site collection to remain in read/write mode during the backup

UseSqlSnapshot: If supported, creates a SQL Server database snapshot, then runs the backup from that snapshot. This reduces the chances of corruption of the backup.

So we can list the current site collections with Get-SPSite:

PS C:\Users\Administrator> get-spsite

Url
---
http://gemini2010
http://gemini2010/my
http://gemini2010/PWA
http://gemini2010/sites/MyDemoSite
http://gemini2010/sites/MyNewCollection

Then we hook a site collection using the –id parameter:

PS C:\Users\Administrator> $site=get-spsite -id http://gemini2010/sites/mydemosite

This way we can verify that we have the right site collection:

PS C:\Users\Administrator> $site

Url
---
http://gemini2010/sites/mydemosite

Once we’re sure we have the right site collection, we can back it up with the PowerShell command:

PS C:\Users\Administrator> Backup-SPSite $site -Path "c:\temp\mydemosite.bak"

You’ll find the .bak file in the path you pointed it to. Now deleting a site collection is far, far too easy:

PS C:\Users\Administrator> Remove-SPSite $site
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-SPSite" on Target "http://gemini2010/sites/MyDemoSite".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y

PS C:\Users\Administrator>

If you have a large site collection, you can use the –GradualDelete parameter to indicate that SharePoint should delete the site collection gradually to reduce the load on the system. Once this completes, if you try any of the URLs in the site collection, you’ll get a 403 Forbidden error.

Finally, we restore the backup, using our new URL.

PS C:\Users\Administrator> Restore-SPSite http://gemini2010/sites/MyNewDemoSite -Path c:\temp\mydemosite.bak

Confirm
Are you sure you want to perform this action?
Performing operation "Restore-SPSite" on Target "http://gemini2010/sites/MyNewDemoSite".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):y

PS C:\Users\Administrator>

Note that you have to restore the Site Collection to a top-level site. Once you run this, your site collection will be back at the new URL, with all subordinate sites mapped appropriately.

Next time we’re going to muck about with renaming sites themselves.

How to rename SharePoint 2010 … Everything!

This is the first in a series of articles to pull together all the various guides around the web to rename SharePoint 2010 databases, servers, services, and other objects. Over the coming weeks I’ll publish these articles:

  • Introduction & Windows PowerShell (this article)
  • Rename the Configuration and Content Databases
  • Rename the State Service Database
  • Rename Alternate Access Mappings
  • Rename Servers
  • Rename SharePoint Site Groups and Sites and IIS Sites
  • Rename IIS Application Pools

Why Rename SharePoint 2010 Objects?

As SharePoint 2010 installations grow in size, admins and DBAs are facing increasing frustration in dealing with Microsoft’s propensity to guarantee unique names by gluing GUIDs on the back of every name. This has gotten to be such an issue that half the reason the AutoSPInstaller script was created was to eliminate the GUIDs where possible. (Note that aside from simply being unattractive, names with GUIDs are a royal pain when you have to write scripts)

A standard SharePoint 2010 installation leaves nasty database names.

However, you may still end up with awkward names for various reasons. There are instructions on how to rename these artifacts scattered around the web; this is my attempt to gather everything into one place.

Windows PowerShell to Rename Content Databases and other SharePoint Objects

If you’re working with SharePoint 2010 and you’re not working with Windows PowerShell, it’s time to start. Virtually all the guidance here is PowerShell-based. I’ll be working with the SharePoint PowerShell provider, which has the SharePoint extensions enabled as well as the path to the 14 hive defined – it makes a great command prompt for SharePoint as well.

Some Windows PowerShell tips before we proceed. You will definitely want to understand piping. The short, simple explanation is that when you use the pipe character ( | ) between commands, it redirects the output of the first command into the second command.

So this script:

Get-SPDatabase | SELECT Name, ID

Returns all the SharePoint databases (Services Databases, Configuration Databases, Content Databases) from the current farm. Think of this as a collection of objects. This collection is then sent to the SELECT command, which will select out the Name and ID properties and list those for all the objects piped to it. Understanding the “way of pipes” will make a huge difference in your productivity with Windows PowerShell.

The other little tidbit about PowerShell and SharePoint objects are how to get the one you want. When you type Get-SPDatabase in the SharePoint Management Shell, you’ll get something looking like this:

image

Using the SharePoint 2010 Management shell.

So how do you get the one you want? The most popular way to pick out a specific object is to use the SELECT-OBJECT cmdlet, like this:

Get-SPDatabase | WHERE-OBJECT { $_.Name -eq "SomeDatabase" }

This line takes that collection of SPDatabase objects output by the Get-SPDatabase cmdlet, and pipes it to the WHERE-OBJECT cmdlet. That takes the collection of objects and uses the statement in curly braces to filter out the database we’re looking for. (“$_” is shorthand for “the current object”)

[amazon_enhanced asin=”1118021983″ container=”AmazonTable” container_class=”alignleft” price=”New” background_color=”FFFFFF” link_color=”000000″ text_color=”0000FF” /]

The problem, of course, is that we have these nasty GUID names to deal with, so we’re going to be copying and pasting from the list of databases. If we have to copy and paste anyway, then we can cut to the chase with this command:

Get-SPDatabase -ID c3c72c94-513b-4ff1-9346-3646f1bdc7ff

This gets us one of the Content Databases – in this case the one for the Central Admin site. The ID is a GUID, and easily selectable to copy and paste from the list. This is the second way to return a specific database, and until we get the names trimmed down, it’s the most straightforward. In addition, the –ID parameter is available for virtually every SharePoint object type, and will obviously always be unique.

With that under your belt, next time we’ll tackle renaming the SharePoint Content Databases, and I’ll address why you should never rename a SharePoint Farm Configuration Database.