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

    Requires Free Membership to View


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.


Const adVarChar = 200
Const MaxCharacters = 255
Set objRecordSet = CreateObject("ADOR.Recordset")
objRecordSet.Fields.Append "Extension", adVarChar, MaxCharacters
Set objFso = CreateObject("Scripting.FileSystemObject")
if Wscript.Arguments.Count = 0 then
    strFolder = objFso.GetAbsolutePathName(".")
     strFolder = Wscript.Arguments(0)
end if
if not objFso.FolderExists(strFolder) then
    Wscript.Echo strFolder & " not found"
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("Extension") = strExtension
        ' skip
    End If
objRecordSet.Sort = "Extension"
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields.Item("Extension")


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:


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.


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.


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.

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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.