Sign In
 
 
Go Search
 
Zach Rosenfield's SharePoint Blog > Posts > Managing SharePoint with PowerShell: Part 2
Managing SharePoint with PowerShell: Part 2

This is the second of an ongoing set of posts about doing SharePoint management with PowerShell. Part 1 is available here – and as always, use these scripts at your own risk.

In this post, I've added just a couple new functions to my "Functions.ps1" script: Get-SPFarm and Get-SPWebApplication. The goal—to be able to quickly answer simple questions about my farm such as "How many site collections do I have?". So on to the good stuff…

Get SPFarm is extremely simple—it just returns the local site. Here's the code

function global:Get-SPFarm{

return [Microsoft.SharePoint.Administration.SPFarm]::Local

}

More interesting is the Get-SPWebApplication function. We get the list of web applications on the farm from the SPFarm.Services. The function below will include the Central Administration Web Application (*Note that the CA application does not have a name, so using Get-SPWebApplication | Select Name will show 3 names—but return 4 web applications!)

function global:Get-SPWebApplication{

Get-SPFarm |% {$_.Services} | where {'$_.TYPEName -eq "Windows SharePoint Services Web Application"'} |% {$_.WebApplications} |% {Write-Output $_}

}

That's it! The full script is available for download from the Script Library. Once the functions are available, you can get great information like the number of site collections per web application using:

get-spwebapplication | %{$_.sites.count}

And the number of site collections in the farm like this:

get-spwebapplication | %{$tot=0}{$tot+=$_.sites.count}{$tot}

Comments

Web Service

Thank you for all your great blog posts!  I know the web is full of blogs on connecting to WSS via Powershell/Web services. Is it something you can discuss?  At the same time, I'm looking for a specific code sample around enumerating a simple list.  I need to export a particular list field into a text file.
Thanks again for being a great blogger to the SharePoint/Powershell community - two great technologies that will turn IT on its head.
at 5/4/2008 11:20 AM

Re: Web Service

I think you're looking for the article I posted on Consuming SharePoint Web Services with PowerShell, which is available here: http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=9

Zach Rosenfield at 5/5/2008 7:37 PM

Listing all Site Collections

To list all the site collections on the farm, use the get-spwebapplication function like this:

get-spwebapplication | %{$_.sites.names}
at 11/28/2008 6:07 AM

Add Comment

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