Skip Ribbon Commands
Skip to main content
Sign In
SharePoint Program Manager, Infrastructure
Zach Rosenfield's SharePoint Blog > Posts > Managing SharePoint with PowerShell: Part 2
April 23
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.
 on 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 on 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}
 on 11/28/2008 6:07 AM

Web app and number of site collections via Powershell

Hi.
I need to be able to list all the web applications and the number of site collections within the web application via powershell.

-get-spweballication command gets me the names and urls of the web application and the get-spwebapplication | %{$_.sites.names} get me the number of site collections.

But i need the information displayed on One table, i currently have to run two seperate powershell cmds to get my desired result.

So ideally i need to 'Somehow' embrace the two cmds together to form one commands where it shows me all the web app and the number of site collections in each web app. But, im not sure how to do it.

Thanks in advance for your help.
 on 10/12/2011 6:20 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) *


Human Test


Checking if you're human: enter "1234" (no quotes)

Attachments