Why PowerShell and SharePoint 2010 Are So Cool


Anybody who has read one of my posts knows that I am a huge PowerShell geek, I tweet about it, I blog about it, I talk about it SO much at my client that my contacts at said client make me add a dollar to the “PowerShell Jar” whenever I use the word “PowerShell.” 🙂

People who don’t ‘touch’ SharePoint may not completely understand why PowerShell and SharePoint are so cool, so I wanted to put together a quick, fairly non-technical post about why they are so cool…

  1. PowerShell allows SharePoint Developers and IT Pros to automate tasks that in the past would’ve required a LOT of scripting – however, we can do it much, much quicker and easier in PowerShell.
  2. PowerShell allows SharePoint IT Pros access to something that in the past we haven’t had easy access to – the SharePoint API or Object Model. (Note: I’m not a developer so if I use a term incorrectly, forgive me. :))

#1 is fairly obvious, but #2 is really where the power comes in.  There are times when an IT Pro is administering SharePoint and he or she needs to do something that just isn’t possible in the UI or through the standard command interfaces.  Having the ability to call methods from the SP Object Model in PowerShell takes away a lot of headaches!

Example: I need to forcefully delete a service application but can’t do so because I blew away the database.  Remove-SPServiceApplication won’t work at this point because that’s the graceful way to do it and I can’t communicate with the service application database.

Resolution:

$app = Get-SPServiceApplication -Identity
$app.Delete()

That’s right, two quick lines of code and I’ve now deleted the old service application that I couldn’t delete using Remove-SPServiceApplication or through the Central Administration interface.

For other examples of using PowerShell and the Object Model to accomplish tasks that in prior (non-PowerShell-enabled) versions were not possible without code – see one of my other recent posts…

Advertisements

Use PowerShell to Create SharePoint Lists and Libraries


This post goes hand in hand with my recent post on retrieving available List and Library templates – although in this PowerShell Function I’m calling the Add method of an SPWeb Object to create lists and libraries.

Most of the ‘code’ in this function comes from the 5 parameters I’m using, but the actual function boils down to the following lines:

Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity $Web
$listTemplate = $SPWeb.ListTemplates[$Template]
$SPWeb.Lists.Add($ListUrl,$Description,$listTemplate)
$list = $SPWeb.Lists[$ListUrl]
$list.Title = $ListTitle
$list.Update()
$SPWeb.Dispose()
Stop-SPAssignment -Global

Essentially I’m just grabbing an SPWeb object from the $Web parameter, I’m then grabbing a ListTemplate from the $ListTemplate parameter, I then call the Add() method to provision the list or library. Once that’s done, I change the Title to the value given in the $ListTitle parameter, hopefully leaving the URL a nice friendly name with no spaces. I then use the Update() and Dispose() methods to update and clean-up, and finally I close out the SPAssignment.

Here’s the complete function:

function New-SPList {
<#
.Synopsis
	Use New-SPList to create a new SharePoint List or Library.
.Description
	This advanced PowerShell function uses the Add method of a SPWeb object to create new lists and libraries in a SharePoint Web
	specified in the -Web parameter.
.Example
	C:\PS>New-SPList -Web http://intranet -ListTitle "My Documents" -ListUrl "MyDocuments" -Description "This is my library" -Template "Document Library"
	This example creates a standard Document Library in the http://intranet site.
.Example
	C:\PS>New-SPList -Web http://intranet -ListTitle "My Announcements" -ListUrl "MyAnnouncements" -Description "These are company-wide announcements." -Template "Announcements"
	This example creates an Announcements list in the http://intranet site.
.Notes
	You must use the 'friendly' name for the type of list or library.  To retrieve the available Library Templates, use Get-SPListTemplates.
.Link
	http://www.iccblogs.com/blogs/rdennis
 		http://twitter.com/SharePointRyan
.Inputs
	None
.Outputs
	None
#>    
	[CmdletBinding()]
	Param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
	[string]$Web,
    [Parameter(Mandatory=$true)]
	[string]$ListTitle,
    [Parameter(Mandatory=$true)]
	[string]$ListUrl,
	[Parameter(Mandatory=$false)]
	[string]$Description,
	[Parameter(Mandatory=$true)]
	[string]$Template
    )
Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity $Web
$listTemplate = $SPWeb.ListTemplates[$Template]
$SPWeb.Lists.Add($ListUrl,$Description,$listTemplate)
$list = $SPWeb.Lists[$ListUrl]
$list.Title = $ListTitle
$list.Update()
$SPWeb.Dispose()
Stop-SPAssignment -Global
}