Prof. Powershell

Value Proposition

If any colleagues aren't convinced of the PowerShell power, this week's trick might convert 'em.

I trust that many of you realize how much value Windows PowerShell brings to the party. A large part of this value is in the form of increased efficiency. In effect, you can accomplish a great deal with little effort. While I understand and appreciate that there is still a PowerShell learning curve, it is not as steep as something like VBScript. And once you have some basic PowerShell literacy you'll be amazed at what you can do.

This time, I want to offer a practical example that you might want to share with your colleagues who still aren't convinced by the PowerShell story. Here is a relatively simple VBScript that creates a CSV report of files in the %TEMP% directory (although it could be any folder):

On Error Resume Next
Set objShell=CreateObject("WScript.Shell")
strDir=objShell.Environment("Process").Item("TEMP")
strFile=objShell.SpecialFolders("MyDocuments") & "\FileReport.csv"
Set objfso=CreateObject("Scripting.FileSystemObject")
Set objFile=objfso.CreateTextFile(strFile,vbTrue)
objFile.WriteLine "File,Size,Created,LastModified"

Call EnumFolder(strDir)

objFile.Close()

Sub EnumFolder(strFolder)
On Error Resume Next
  Set objFolder=objfso.GetFolder(strFolder)

  For Each strFile In objFolder.Files
    objFile.WriteLine strFile.path &"," & strFile.size & "," &_
    strFile.DateCreated & "," & strFile.DateLastModified
  Next

  For Each objSub In objFolder.SubFolders
    Call EnumFolder(objSub.Path)
  Next
End Sub

This script uses the FileSystemObject to query the specified folder and create the CSV file. The script is about 25 lines and must be saved to a text file and executed. Here is a one-line PowerShell command that does the same thing:

dir $env:temp -recurse -force -ea SilentlyContinue |
where {$_ -is [system.IO.FileInfo]} |
select-object Fullname,Length,CreationTime,LastWriteTime |
Export-Csv -Path "$env:userprofile\documents\FileReport2.csv" -NoTypeInformation

I've broken some of the expressions to make it easier to follow. There's very little in this command that a PowerShell beginner couldn't come up with. You'll get practically the same results. The only difference is that the CSV header will be different because property names are different. But that too, can be remedied with only a little more work:

dir $env:temp -recurse -force -ea SilentlyContinue |
where {$_ -is [system.IO.FileInfo]} |
select @{Name="File";Expression={$_.Fullname}},`
@{Name="Size";Expression={$_.Length}},@{Name="Created";Expression={$_.CreationTime}},`
@{Name="LastModified";Expression={$_.LastWriteTime}} |
Export-Csv -Path "$env:userprofile\documents\FileReport2.csv" -NoTypeInformation

If you need a report like this frequently, you can save this one-line command into a .ps1 script file and run it. Even better: Let's say you need reports like this for a number of folders on a regular basis. Take advantage of PowerShell's modular nature and achieve even more efficiency. There are many ways you could handle this; here's one possible script:

Param([string]$path=$env:temp,
  [string]$logpath="$env:userprofile\documents"
      )

#change any \ in the path to _
$folder=$path.Replace("\","_")
#remove any :
$folder=$folder.Replace(":",$null)
#define a report path
$ReportPath=Join-Path $logPath "$folder.csv"
write-host "Logging results to $ReportPath" -ForegroundColor Green
dir $path -recurse -force -ea SilentlyContinue |
where {$_ -is [system.IO.FileInfo]} |
select @{Name="File";Expression={$_.Fullname}},`
@{Name="Size";Expression={$_.Length}},`
@{Name="Created";Expression={$_.CreationTime}},`
@{Name="LastModified";Expression={$_.LastWriteTime}} |
Export-Csv -Path $reportPath -NoTypeInformation

Now all you need is a text file of directory paths. Use Windows PowerShell to go through the list and pass each one to your script:

Get-content folders.txt | foreach {c:\scripts\FolderReport.ps1 -path $_}

Consider how much VBScript it would take to achieve the same result. With PowerShell, you get something that's flexible and interactive.

Here's my challenge to you: The next time you have an administrative automation need, see how long it takes you to accomplish it using VBScript and then compare it to how long it takes using Windows PowerShell. At the end, which is easier to maintain in the long run and offers the most flexibility and value to your organization?

Don't forget: If you need some PowerShell help to drop by the forums at ScriptingAnswers.com.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular