Windows PowerShell: List Properties and Values of an Object

This may be obvious to some, but it took me a while to figure out.

If you want a list of all the properties and their values for an object in PowerShell, use Format-List like this:

$object | Format-List *

Hope this helps someone else!

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.

SharePoint 2010: Rename the SharePoint State Service Database

Continuing on a quest to rename everything in SharePoint. You can find the first article here.

Rename the SharePoint State Service Database

The State Service in SharePoint 2010 is a service application that manages user state across browser requests. The SharePoint State Service is required by InfoPath Forms Services, the Chart Web Part, and certain Visio 2010 scenarios. You can find the State Service in Central Admin under Application Management – Manage Service Applications. There will be one or more State Service applications and associated proxies.

As usual, if the state service was created by the SharePoint Configuration Wizard, or you create it without editing the database name, the database will be created with a GUID on the end, such as:

StateService_5536604e93df4369a916cf408e0c3866  (ew!)

So let’s check out our state service in PowerShell. Open the SharePoint 2010 Management Shell (Start –> All Programs –> Microsoft SharePoint 2010 Products –> SharePoint 2010 Management Shell). To start, try a simple:

Get-SPDatabase | Select Name

This will list the SharePoint databases, and you should easily spot your State Service database. If for some reason you don’t know the name of the State Service Database, you can list all state service databases with this command:

Get-SPDatabase | Where-Object { $_.Type -eq "Microsoft.Office.Server.Administration.StateDatabase" }

Every SharePoint object has an ID (a GUID), and the easiest way to get a reference to a SharePoint object is by using that ID. You can get the ID for databases with this command:

Get-SPDatabase | SELECT Name, ID

Once you find the ID for your State Service Database, you can get a handle on it with this command:

$db=Get-SPDatabase -id {guid}

Next (and this is a common step with SharePoint Service databases; the trick is always figuring out how) you have to dismount the database:

Dismount-SPStateServiceDatabase -Identity <DatabaseID>

For “DatabaseID” you can either paste in the GUID we copied earlier, or you can use the $db variable as a hook to the database itself. $db.Id won’t work unless you cast it to a string first.

You’ll be asked to verify that you want to do this:

Confirm
Are you sure you want to perform this action?
Performing operation "Dismount-SPStateServiceDatabase" on Target "SP_Dev_StateService_DB".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):

A simple “Y” and enter will dismount the database. Now you can go to SSMS to rename the database — right-click on the database and select “Rename” or in T-SQL:

ALTER DATABASE oldName MODIFY NAME = newName

After you’ve renamed the database, then mount it again with the command

Mount-SPStateServiceDatabase -Name "<DatabaseName>" -DatabaseServer "<ServerName>"

And you’re all set.

If you completely screw up the database somehow, or feel it’s corrupted, there’s no critical data in the database – simply delete it and create a new one with the command:

New-SPStateServiceDatabase -Name "<StateServiceDatabase>" -ServiceApplication $serviceApp

Which will create a state service database and associate it with the State Service.