Cryptic Code Commanders Rejoice with Windows PowerShell

Graphical User Interfaces a.k.a. GUIs (gooeys) are familiar to most business computer users. Modern day operating systems present users with a graphical representation of items that can be browsed, usually with drop down menus and context menus for accessing advanced options.

More accomplished users rely on the sophisticated functionality of a Command Line Interface (CLI) to accomplish demanding tasks.

A Command Line Interface (CLI), such as the ubiquitous DOS command prompt or Windows PowerShell, use a different approach to expose functionality, because it does not expose menus or graphical hints to assist the user. Users have to master command names and syntax to implement this method of computer administration and configuration. Power Users may type complex commands that are equivalent to the features in a GUI environment.

Terse command names were generally chosen by Operating System developers over clear ones. Beginner command line users are presented with confusing gibberish in error messages if the command is not perfectly constructed. Can you say DIR, RD, MD and CLS? These TLAs (three letter acronyms) are the command line equivalent of using Windows File Manager to create and manage folders and files.

Windows PowerShell was designed to take advantage of a Power User’s previous experience with CLIs. The character of PowerShell is that of a command line Object Oriented scripting language and is very particular about why and how you structure your commands.

PowerShell Syntax Basics

The PowerShell command line environment processes “Commandlets” or micro programs on the operating system. You may find that specific commandlets must be loaded into the PowerShell environment before they can be accessed. Commandlets consist of a Verb-Noun pair separated with a hyphen (no spaces please). A basic command might look like this: Get-SomethingOrOther. The verb Get instructs PowerShell to retrieve the element specified by the noun after the hyphen. The resulting object can be loaded into a pipeline with the pipe character to support additional operations. The following script is a good example of using PowerShell to manage SharePoint installations. The script enumerates all the Sites and Site Collections on a SharePoint WebApp and returns the Title, the Template that was used to create the site and the Last Modified Date. The results are then output to a .CSV file on the desktop. You can quickly determine from this exercise which sites are old and/or rarely accessed and maybe free up some space on your content databases.

  • 1 # Farm Enumerator PS1 Script
  • 2 set-executionpolicy remotesigned
  • 3 Add-PSSnapin Microsoft.SharePoint.Powershell
  • 4 Get-SPWebApplication “http://serverName” |
  • 5 Get-SPSite -Limit ALL |
  • 6 Get-SPWeb -Limit ALL |
  • 7 Select URL ,Title, WebTemplate,LastItemModifiedDate |
  • 8 Sort LastItemModifiedDate |
  • 9 Export-CSV desktop\farmEnumerated 

Legend:

  • 1 The # is the PowerShell operator for commenting
  • 2 The equivalent of running PowerShell as an administrator
  • 3 Install the SharePoint commandlets for PowerShell
  • 4 Get a context to the IIS root site and put it into a pipeline
  • 5 Return all Site Collections and update the pipeline
  • 6 Return all child Web Sites and update the pipeline
  • 7 Determine what columns to return to the script and update the pipeline
  • 8 Sort the results by Last Item Modified Date and update the pipeline
  • 9 Return the results of the query as a Comma Separated Values file on the desktop