SharePoint 2010 : Understanding Windows PowerShell Concepts (part 3)

2/5/2011 5:54:35 PM

Verbs and Nouns

Every built-in PowerShell cmdlet is composed of a verb and noun (Verb-Noun)—for example, Get-SPSite. The verb part of a cmdlet indicates an operation that will be performed, and the noun indicates the object on which the operation will be performed. The Get-SPSite command gets a SPSite object (SharePoint site collection object).

The other common PowerShell operations (verbs) are Set- (modifies object), Remove- (deletes object), and New- (creates a new object).

Most SharePoint cmdlets come with these four verb-noun combinations. However, some objects might have more or less cmdlets depending on the object type—for example, the SPSite object also comes with Backup-, Move-, and Restore- cmdlets.

Working with Variables, Function, and Cmdlets

You can use PowerShell variables to store results from any executed command. The following example demonstrates how to use variables:

#$SiteCollection = Get-SPSite "http://portal.companyabc.com"

The preceding given command saves site collection (from http://portal.companyabc.com) to a variable $SiteCollection; the variable can then be used to retrieve properties. The preceding example shows how to retrieve Url and Owner properties and display them in the shell.

The value stored to $SiteCollection is a SharePoint-specific .NET Framework object: SPSite and exposes all available properties as in any other .NET language.

To learn more about SharePoint 2010 SDK, see the following URL:


PowerShell Command Piping

In real life, people use pipes to transfer goods (for example, oil) from one end to another. PowerShell pipes are similar because they enable you to easily transform, modify, or pass the result of one cmdlet to another.

Let’s say we want to list all site collections in a SharePoint farm. Get-SPSite cmdlet displays all the site collections, but in large farms it might returns hundreds or thousands of results. If Get-SPSite is combined with a filter command (over pipe), it will return only specific site collections that require attention.

Get-SPSite | Where {$_.Url –eq "http://portal.companyabc.com"}

By using the pipe “|”, we see results (all site collections) of the Get-SPSite cmdlet that are passed to the Where pipe, which dictates that only site collections whose URL equals to http://portal.companyabc.com will be released to the next cmdlet in pipeline.

The preceding example has only one pipe, but additional pipes could be used:

Get-SPSite | Where {$_.Url –like "*portal*"} | Sort RootWeb | Select RootWeb, Url

The preceding command lists all the sites collections that contain the word portal in their URL and then sorts them by RootWeb property and displays the results in table format with two columns RootWeb and Url.

Figure 5 shows the results of running the preceding code in the SharePoint 2010 Management Shell.

Figure 5. PowerShell returns a list of site collections with a word portal in the URL.

Formatting Results

Results retrieved from PowerShell cmdlets are usually displayed in a tabular form but can be further customized by adding the format command. Typical formatFormat-List, Format-Table, and Format-Wide. commands are

Get-SPSite | Select RootWeb, Url, Owner | Format-Table

The preceding command will list all the site collections and display results in table format. (We selected RootWeb, Url, and Owner properties of site collection to be displayed in the table.)

In addition to formatting results, you can choose a different output destination than a default one (shell screen). One of the useful output destinations is GridView:

Get-SPSolution | Select * | Out-GridView

The preceding command displays all installed SharePoint solutions in a GridView format, as shown in Figure 6. If you do not have installed solutions, you could combine Out-GridView with other cmdlets, such as Get-SPSite.

Figure 6. Displaying results of PowerShell cmdlet in a GridView format.

PowerShell—The New STSADM

With the release of SharePoint 2010, PowerShell becomes the most important tool for SharePoint administration automation. Every administration operation is first delivered for PowerShell, and some operations (for example, advanced customization during installation, multitenancy administration, and so on) are available via PowerShell only. The built-in cmdlets have much more options than Central Administration UI and STSADM commands.

Administrators can still combine all three interfaces; an administrator that only periodically needs to create a single site collection should probably still use Central Administration, but PowerShell should be the first choice when it comes to administration and automation.

  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 2)
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 1)
  •  Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 3) - Discovery
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 2) - Messaging Records Management
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 1) - Exchange 2010 Archiving
  •  Managing Exchange Server 2010 : Role Based Access Control (RBAC)
  •  SharePoint 2007 : See What Files or List Items Are Checked Out to You
  •  SharePoint 2007 : Publish a File or List Item
  •  SharePoint 2007 : Use the Datasheet View to Add, Edit, or Delete Items and Files
  •  Exchange Server 2007 : Configure the Client Access Server - Manage Exchange ActiveSync
  •  Exchange Server 2007 : Configure the Client Access Server - Manage OWA
  •  Exchange Server 2007 : Configure the Client Access Server - Administer the CAS Role
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 6)
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 5) - Reviewing Security Settings in SPCA
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 4) - Reviewing Backup and Restore Settings in SPCA
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 3) - Administering Monitoring Tasks in SPCA
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 2) - Administering System Setting Tasks in SPCA
  •  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 1) - Administering Application Management Tasks in SPCA
  •  Sharepoint 2007: Recover a Deleted File or List Item
    Top 20
    Exchange Server 2007 : Configure the Client Access Server - Manage OWA
    Azure Programming Considerations
    Developing an SEO-Friendly Website : Redirects
    Sharepoint 2007: Create a New Document
    Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
    MediaElement in Silverlight
    Programming .NET Security : Cryptography Explained (part 2)
    Programming .NET Security : Keyed Hashing Algorithms Explained
    Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
    Windows 7 : Using Desktop Gadgets (part 1) - Using the Calendar gadget
    Windows Server 2008 : Manage Hyper-V Remotely
    Windows Server 2008 : Utilize System Center VMM
    Windows Server 2008 : Configuring and Monitoring Terminal Service Resources
    iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
    Using Images in XAML
    Windows 7 : Managing the Boot Sector for Hard Disk Partitions
    SQL Server 2008 Instance Architecture
    Fine-Tuning Windows 7’s Appearance and Performance : Balancing Appearance and Performance
    Positioning Elements in XAML
    Using the Data Cache