PowerShell, VBscript and Batch Files: File Management Comparisons

PowerShell, VBscript and Batch Files: File Management Comparisons

Automation and scripting for Windows has been around ever since Windows was Windows.  First there were batch files, then VBscript (VBS) and now PowerShell.  Sure, there have been some others along the way but, for the most part, good ol’ batch, VBS and PowerShell have been the mainstays.  Over the years, scripts have become not only easier to write but more powerful as well. You no longer have to be a developer to understand Windows scripting with PowerShell.  This is great news for IT pros but not everyone is up to snuff on the latest Windows PowerShell cmdlets. For people just dipping their toe in the PowerShell pool, it’s important to have a shallow end to slowly wade into instead of pushing into the deep end head first.

The PowerShell “shallow end” is demonstrating the old method of automating Windows (batch and VBS) vs. the new method (PowerShell).  It’s useful to see side-by-side comparisons rather than saying “Discard that batch and VBS knowledge you’ve learned over a couple decades. It’s worthless now.”  Rather, show an apples-to-apples comparison of various problem-solving techniques and how to solve them in each language.  When shown side-by-side like this the best method will clearly show through.

Let’s get started by focusing on file management.  File management is a common ground that all methods have experience with. We’ll go over a few routine activities when managing files using each method to let you decide which is the easiest.  We’ll start from simple tasks such as managing single files all the way to creating a CSV file of a folder’s contents.  Pay close attention as the harder the concepts get the more you’ll see batch files and VBscript code break down while PowerShell will maintain a consistent ease of use.

First up is managing a single file.  In this instance, each option appears similar in syntax except for the always obscure VBscript.

 

Copying/Moving/Removing a Single File

Batch:

copy C:\MyFile.txt C:\SomeOtherFolder

move C:\MyFile.txt C:\SomeOtherFolder

del C:\MyFile.txt

 

VBscript:

Set objFs = CreateObject("Scripting.FileSystemObject")

objFs.CopyFile "C:\MyFile.txt", "C:\SomeOtherFolder\"

objFs.MoveFile "C:\MyFile.txt", "C:\SomeOtherFolder\"

objFs.DeleteFile "C:\MyFile.txt"

 

Powershell:

Copy-Item -Path ‘C:\MyFile.txt’ -Destination ‘C:\SomeOtherFolder’

Move-Item -Path ‘C:\MyFile.txt’ -Destination ‘C:\SomeOtherFolder’

Remove-Item -Path ‘C:\MyFile.txt’

 

Listing Folder Contents

Next up is getting the contents in a folder.  Again, batch and PowerShell are both one-liners but what’s up with VBscript? Now you know why so many VBscripters rejoiced when PowerShell came along.

Batch:

dir C:\

 

VBscript:

Set objFs = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFs.GetFolder(“C:\”)

Set colFiles = objFolder.Files

For Each objFile in colFiles

    Wscript.Echo objFile.Name

Next

 

Powershell:

Get-ChildItem -Path C:\

 

Filtering Files in a Folder Based on a File Name

You probably already know how to list files in a folder, but what if we only need some files that match a specific pattern?

Batch:

dir C:\*somestringinsideafile*

 

VBscript:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where Name LIKE 'C:\%somestringinsideafile%\'")

For Each objFile in colFiles

    Wscript.Echo objFile.FileName

Next

 

Powershell:

Get-ChildItem -Path C:\*somestringinsideafile*

Again, batch and PowerShell are very similar but VBscript goes off the rails with complexity because we’re forced to use WMI.

 

Finding Files in a Folder and Creating a CSV

Finally, let’s really put each method to the test and add some complexity.  In this instance, I want to find the file names in a folder and then generate a report for my manager with these file names and the file size inside an easy to read CSV file.

Batch:

(for %F in (C:\*) do @echo "%~dpF","%~nxF",%~zF)>>Files.csv

 

VBscript:

Set objFs = CreateObject("Scripting.FileSystemObject")

Set objFile = objFs.CreateTextFile(“C:\Files.csv”)

Wscript.sleep(2000) 'Pause while file is being created

Set objFile = objFs.GetFile(“C:\Files.csv”)

Const ForWriting = 2

Set outputFile = objFs.OpenTextFile(“C:\Files.csv”, ForWriting)

Set objFolder = objFs.GetFolder(“C:\”)

Set colFiles = objFolder.Files

For Each objFile in colFiles

    OutputFile.Write “C:\,” & objFile.Name & “,” & objFile.Size

    Wscript.Echo “C:\,” & objFile.Name & “,” & objFile.Size

Next

OutputFile.Close

 

PowerShell:

Get-ChildItem -Path C:\ -File | Select-Object Directory,Name,Length | Export-Csv C:\csv.csv -Append -NoTypeInformation

 

The Takeaway

If you’re still scripting with batch files or VBscript this side-by-side comparison should have resonated with you.  It should show that:

  1. VBscript’s return on time figuring out how to automate something is terrible compared to batch or PowerShell.

  2. You can take what you’ve learned scripting with batch files and easily move those skills over to PowerShell.

  3. As script complexity increases, batch, and especially VBscript’s code, complexity increases exponentially vs with PowerShell only slightly.

Decide for yourself.  Which language is easier to read?  Which would be easier to use for your day-to-day tasks? And finally, which language is getting more support these days?  Once you answer these questions, the language of choice should be clear.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish