Wednesday, November 21, 2012

Using PowerShell for SharePoint 2010

Background

I have walked simultaneously in the worlds of Windows and Unix/Linux/Apple OS X for years. In *nix, I prefer BASH (GNU Bourne-Again SHell) for administration. However, until PowerShell, Windows had nothing that could really compare to Bash. Microsoft had shells: first the COMMAND.COM in MS-DOS/Windows 9x and cmd.exe in Windows NT, then Windows Script Host introduced with Windows 98. But these were severely limited, and you had to rely on a lot of manual labor in the GUI. This is a major reason why *nix admins generally have managed a lot more systems than have Windows admins, requiring companies with Windows servers to hire more admins. (Then there's the whole blue screen of death issue, but I digress…)

PowerShell changed all of that beginning (officially) with its release to the Web (RTW) in 2006. Like BASH and other *nix shells, PowerShell uses pipelines to pass the output of one command as the input to another. But unlike standard *nix pipelines, PowerShell employs an object pipeline, with data passed as fully-typed objects rather than simple character streams.

Overview

PowerShell can execute the following types of commands:
  • cmdlets ("command-lets"), which are single-feature commands that manipulate objects
  • PowerShell scripts (*.ps1 files)
  • PowerShell functions
  • standalone executable programs

PowerShell for SharePoint 2010

PowerShell for SharePoint 2010 gives you access to the entire SharePoint object model as well as the entire .NET framework. What I love most about it is the ability to write handy scripts to do repeatable actions (or to share with clients for their own use). This is handy to system administrators, but it is especially useful for someone like me, who is an architect with very little time to get their hands dirty.

SharePoint 2010 Management Shell

To work with SharePoint in PowerShell, you must register Microsoft.SharePoint.PowerShell.dll inside PowerShell. If you use the SharePoint 2010 Management Shell shortcut on the server, this registration is automatic with an Add-PSSnapin command in the startup script. This shortcut should generally be "run as administrator."

Windows PowerShell ISE

But what if you want to write your own PowerShell functions and scripts within .ps1 files? You can use any text editor. But if you are writing serious scripts, you should use the Windows PowerShell ISE (Integrated Scripting Environment). The only initial problem is that this tool does not automatically load Microsoft.SharePoint.PowerShell.dll by default.

Using Windows PowerShell ISE for SharePoint Scripts

There are a few ways to make that happen. I use the following method{{ref|Source: http://blogs.msdn.com/b/tomwis/archive/2010/01/05/enable-sharepoint-powershell-commandlets-in-the-powershell-ise.aspx}} to add the snap-in to my Windows PowerShell ISE profile:
  1. Open the Windows PowerShell ISE.
  2. Run the set of commands below by pasting them and pressing Enter.
  3. Close Windows PowerShell ISE.
  4. Reopen Windows PowerShell ISE.

Commands to Run in Windows PowerShell ISE

if (!(test-path $profile ))
{
  new-item -type file -path $profile -force
}
  $cmd = 'if((Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
  Add-PSSnapIn "Microsoft.SharePoint.Powershell"
}'
out-file -FilePath $profile -InputObject $cmd -Append

Now whenever you open the Windows PowerShell ISE, you will be able to take full advantage of the SharePoint snap-in.

SharePoint PowerShell Resources


Happy scripting!

No comments:

Post a Comment