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.
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
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.
|
Group-Object
Select-Object
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
Enterprise Server Strategies for the CIO
Join the conversationComment
Share
Comments
Results
Contribute to the conversation