What makes Windows PowerShell unique?

Find out why you should learn and use Windows PowerShell -- even if you are already familiar and comfortable with another scripting language.

A question administrators often ask is "Why Windows PowerShell?" They wonder why they should learn this particular scripting language when they already know – and effectively use – other languages like VBScript or Perl.

The simple answer: Interactive objects. This means more than just having access to objects. The critical point here is that everything in PowerShell is an object.

Although the initial power might not be clear, once you understand how to use PowerShell you will never look at scripting the same. For example, below is code for the task "List unique file extensions" in both VBscript and PowerShell.

VBScript

Const adVarChar = 200
Const MaxCharacters = 255
Set objRecordSet = CreateObject("ADOR.Recordset")
objRecordSet.Fields.Append "Extension", adVarChar, MaxCharacters
objRecordSet.Open
Set objFso = CreateObject("Scripting.FileSystemObject")
if Wscript.Arguments.Count = 0 then
    strFolder = objFso.GetAbsolutePathName(".")
else
     strFolder = Wscript.Arguments(0)
end if
if not objFso.FolderExists(strFolder) then
    Wscript.Echo strFolder & " not found"
    Wscript.Quit
end if
Set objFolder = objFso.GetFolder(strFolder)
For Each objFile In objFolder.Files
    strExtension = objFso.GetExtensionName(objFile)
    objRecordSet.Find "Extension = '" & strExtension & "' "
    If (objRecordSet.BOF = True) OR (objRecordSet.EOF = True) Then
        ' Record not found
        objRecordSet.AddNew
        objRecordSet("Extension") = strExtension
        objRecordSet.Update
    Else
        ' skip
    End If
    objRecordSet.MoveFirst
Next
objRecordSet.Sort = "Extension"
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields.Item("Extension")
    objRecordSet.MoveNext
Loop

PowerShell

Get-childitem | group-object extension | select-object name

While the code isn't that complicated in VBScript, it is so trivial in PowerShell that it only requires one line of code. But how exactly did PowerShell objects help in this situation? Let's start by breaking down the line of code:

Get-ChildItem

This is a built-in cmdlet that list files and folders in a given path. The key here is that it doesn't just return a string with the information you want but instead returns a collection of robust objects. These objects are then passed along to either the console or to another cmdlet if the | is used (explained in the next bullet).

In the above example, the objects were piped to another cmdlet called group-object. If we had just called Get-ChildItem, Powershell would have sent the objects to the console.

|

This is the pipe operator. It is used in most shells to pass data from one command to another. PowerShell uses this to pass the objects outputted to other cmdlets. It is important to note that the entire object is passed -- not just what you see.

Group-Object

This takes a collection of objects and sorts them by the specific property. In this case, the property is Extension, but it could have been any of the properties available.

Select-Object

This allows you to pick off the object you want to keep.

But that's not all. If you wanted to not only sort by extension, but also by total file size, that can also be done in only one line:

Get-Childitem -Recurse -ea 0 | ?{!$_.PSIsContainer} | Group-Object Extension | select name,@{n="GroupSize";e={($_.group | measure-object -sum Length).sum}} | Sort-Object -Descending GroupSize -ea 0

If you're newer to Windows Powershell, the above example may be a bit complicated. Here is the same task in smaller bites:

$ScriptBlock = @{n="GroupSize";e={($_.group | measure-object -sum Length).sum}} $FilesByExtension = Get-Childitem -Recurse -ea 0 | ?{!$_.PSIsContainer} | Group-Object Extension $FilesByExtension | select-object Name,$ScriptBlock | Sort-Object -Descending GroupSize -ea 0

This example can be taken further and put into a Microsoft Excel spreadsheet. (Check out this Cool demo video and code at BSonPoSH.com to see the full PowerShell example).

Overall, the object nature of Windows PowerShell is powerful and truly makes it a different scripting language.

ABOUT THE AUTHOR
Brandon Shell has been in the IT industry since 1994. He started out as a PC tech and general fix-it guy for numerous companies. In 2000, Shell joined Microsoft as contractor for the Directory Services team, and he became a full-time employee in 2002. In 2004, Shell left Microsoft to pursue a new position focusing on his real passion, PowerShell. In 2007, he joined the PowerShell MVP ranks, and Shell has spent the past several years building his PowerShell knowledge and helping others build theirs.

This was first published in January 2010

Dig deeper on Microsoft Group Policy Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close