PowerShell How-To

Comparing Common Actions in Batch, VBscript and PowerShell

If you haven't learned PowerShell yet you're way behind the curve. Now's the time to catch up with the rest of your industry peers! If you're still slumming it with batch files and VBscript and haven't "had the time" to learn PowerShell yet, you're going to need a bridge. You're going to need a legacy script to PowerShell bridge. Once you're able to relate batch and VBscript code to how PowerShell does it, you'll have a much easier time doing the transition.

There are a few scenarios that I've seen over and over that can be accomplished with just about any scripting language. Let's talk about a couple of those examples and compare how batch, VBscript and PowerShell accomplish the same task.

Executing a Process
Executing a process is a common task that admins must accomplish. Unfortunately, even in PowerShell, you'll find that you have to run external commands every now and then. In VBscript and PowerShell there are numerous ways to start a process but I've chosen to take the path of least resistance and show you the easiest way possible.

Batch: Batch files are essentially a whole bunch of cmd.exe commands.  How do you launch an external process in cmd.exe?  Yep, just type it in. Done.

notepad.exe

VBscript: The authors of VBscript seem to like making an admin's life miserable with it's arcane way of doing things. Starting a process is no different. To start a process in VBscript requires instantiating a Wscript.Shell object then invoking the Run() method. This is by far the hardest way just to start a process.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run ("notepad.exe")

PowerShell: Since PowerShell can be run interactively on the console it can mimic batch's behavior.  As with batch, if you want to start a process just simply say so.

notepad.exe

You can also use the Start-Process cmdlet which gives you a lot more control over the process's termination and exit code.

Copying/Moving a Single File
We all have to move files around at some point. Each language has its way of getting files from point A to point B.

Batch: Batch uses the syntax command sourcefile destinationfile to copy or move files. The command is simply copy or move.

copy C:\MyFile.txt C:\SomeOtherFolder
move C:\MyFile.txt C:\SomeOtherFolder

VBscript: VBscript being VBscript makes thing a little more difficult but not too bad.  As with anything in VBscript you'll first have to instantiate an object. This time, it's the FileSystemObject. This is an object that groups common file system activities into a few different methods. This object allows you to copy files with the CopyFile() method and move files with the MoveFile() method.

Set objFs = CreateObject("Scripting.FileSystemObject")

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

PowerShell: PowerShell, again, is similar to batch when working interactively in the console. PowerShell has cmdlets to copy and move various items--files for example--with the Copy-Item and Move-Item cmdlets.  It uses -Path and -Destination parameters to specify the source and destination. However, since PowerShell has aliases and uses parameter position you could technically mimic batch and it would work as well.

Copy-Item -Path 'C:\MyFile.txt' -Destination  'C:\SomeOtherFolder'
Move-Item -Path 'C:\MyFile.txt' -Destination 'C:\SomeOtherFolder'
Remove-Item -Path 'C:\MyFile.txt'

I hope this was able to give you a sense on the kinds of differences each language. Once you get more into PowerShell you'll find it acts as a rough combination of both VBscript and batch.  Since everything in PowerShell is an object you're still working with objects like in VBscript but all of the under-the-covers stuff is done for you. At the same time, you still get that familiarity with batch because of all the useful aliases provided.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular