Sign In
 
 
Go Search
 
Zach Rosenfield's SharePoint Blog > Posts > SP 2010: Intro to PowerShell Part 3
SP 2010: Intro to PowerShell Part 3

This is another installment in the intro to PowerShell “series”.  I’m going to start using the "Intro to PowerShell" header to differentiate these PowerShell intro topics from other 2010 discussions.

This particular posts is still on using the “pipeline” with SharePoint 2010 PowerShell cmdlets… More advanced stuff will be coming in future posts.

1.       Get all sites from all content db’s in a web application with a particular name
PS>  Get-SPWebApplication "SharePoint - 80" | Get-SPContentDatabase | Get-SPSite –limit all



2.       Get all sites from the 1st database attached to the web application
PS> Get-SPWebApplication | Get-SPContentDatabase |select -First 1 | Get-SPSite –limit all

 

3.       You can also mix SharePoint cmdlets and Windows PowerShell cmdlets together in a single pipeline.  Here we get a sorted list of all site collections from all content databases that start with a particular phrase (WSS_):
PS> Get-SPContentDatabase | Where{ $_.Name –like "WSS_*"}  | Get-SPSite –limit all | Sort URL –Descending

 

4.       The Foreach-Object cmdlet is useful for commands where something other than the Identity is being piped into the object.  For example, when enabling a feature on one or more sites you can use the “Foreach-Object” (which can be aliased with a percent sign ‘%’) to iterate through sites and call a specific command.  For example I can run this command to enable Ratings on one site collection (I’ve left “whatif” on to have it show the behavior—a good thing to always run first!):

PS> Get-SPSite "http://MySharePoint" | ForEach-Object { Enable-SPFeature "Ratings" -url $_.url -whatif }



Or I can run this on all site collections:

PS> Get-SPSite -limit ALL | ForEach-Object { Enable-SPFeature "Ratings" -url $_.url -whatif }

 

5.       Last item for this post: calculated properties.  This is a functionality of the “Select-Object” cmdlets from PowerShell—but very useful for getting data out of SharePoint that may exist at multiple “levels” of SharePoint.  Getting the Usage data from a site collection is a good example—here’s a demonstration:

#Get Usage for a set of site collections—shows a collection for usage

Get-SPSite | Select URL, Usage


#Use a calculate property to “reach inside” the Usage property to get a specific value

Get-SPSite | Select URL, @{Expression={$_.Usage.Storage}}


#Make it cleaner with titles and formatting

Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} GB" -f ($_.Usage.Storage/1000000)}}


#Then do something with the data!  This new "object" is still pipelineable and actionable (here with GridView)

Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} GB" -f ($_.Usage.Storage/1000000)}}, @{Name="Quota"; Expression={"{0:N2} GB" -f ($_.Quota.StorageMaximumLevel/1000000)} } | Out-GridView -Title "Sites w/Usage"

 

And below you can see this all in action (I use a limit to make this fit on one screen. Use “–Limit ALL” to have the script display all site collections):

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Comments *


Name (required) *


Attachments
 
 
   
Real Time Web Analytics