Tuesday, April 7, 2015

PowerShell, VBscript and Batch information: File management Comparisons

Automation and scripting for home windows has been round ever because windows became windows.  First there were batch files, then VBscript (VBS) and now PowerShell.  sure, there have been some others along the manner however, for the most part, first rate ol' batch, VBS and PowerShell were the mainstays.  over the years, scripts have develop into no longer handiest more convenient to jot down however extra potent as smartly. You not have to be a developer to be mindful windows scripting with PowerShell.  here is top notch news for IT pros however now not all and sundry is up to snuff on the latest windows PowerShell cmdlets. For people just dipping their toe in the PowerShell pool, it's vital to have a shallow end to slowly wade into as a substitute of pushing into the deep conclusion head first.

The PowerShell "shallow end" is demonstrating the historical components of automating windows (batch and VBS) vs. the new formulation (PowerShell).  It's positive to look facet-via-side comparisons as opposed to saying "Discard that batch and VBS expertise you've realized over a couple many years. It's nugatory now."  reasonably, display an apples-to-apples assessment of quite a few issue-fixing techniques and the way to resolve them in each language.  When shown side-by-facet like this the best components will clearly exhibit via.

Let's get all started by means of specializing in file administration.  File management is a typical floor that each one methods have adventure with. We'll go over just a few events activities when managing files using each method to help you decide which is the least difficult.  We'll start from standard projects akin to managing single information the entire option to making a CSV file of a folder's contents.  Pay shut attention as the tougher the ideas get the more you'll see batch files and VBscript code wreck down whereas PowerShell will maintain a consistent ease of use.

First up is managing a single file.  in this illustration, each option seems equivalent in syntax aside from the at all times imprecise VBscript.

Batch:

copy C:\MyFile.txt C:\SomeOtherFolder

stream 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:

reproduction-merchandise -course 'C:\MyFile.txt' -destination 'C:\SomeOtherFolder'

circulation-item -path 'C:\MyFile.txt' -destination 'C:\SomeOtherFolder'

eradicate-item -course 'C:\MyFile.txt'

next up is getting the contents in a folder.  once more, batch and PowerShell are each one-liners but what's up with VBscript? Now you be aware of why so many VBscripters rejoiced when PowerShell came alongside.

Batch:

dir C:\

VBscript:

Set objFs = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFs.GetFolder("C:\")

Set colFiles = objFolder.data

For each objFile in colFiles

    Wscript.Echo objFile.name

next

Powershell:

Get-ChildItem -direction C:\

You doubtless already comprehend how to checklist information in a folder, but what if we best need some information that healthy a particular pattern?

Batch:

dir C:\*somestringinsideafile*

VBscript:

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

Set colFiles = objWMIService.ExecQuery("choose * from CIM_DataFile where name LIKE 'C:\%somestringinsideafile%\'")

For each and every objFile in colFiles

    Wscript.Echo objFile.FileName

subsequent

Powershell:

Get-ChildItem -path C:\*somestringinsideafile*

once again, batch and PowerShell are very identical however VBscript goes off the rails with complexity as a result of we're forced to make use of WMI.

at last, let's in fact put each formulation to the look at various and add some complexity.  during this example, I are looking to discover the file names in a folder and then generate a report for my supervisor with these file names and the file size interior a straightforward to study CSV file.

Batch:

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

VBscript:

Set objFs = CreateObject("Scripting.FileSystemObject")

Set objFile = objFs.CreateTextFile("C:\data.csv")

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

Set objFile = objFs.GetFile("C:\information.csv")

Const ForWriting = 2

Set outputFile = objFs.OpenTextFile("C:\files.csv", ForWriting)

Set objFolder = objFs.GetFolder("C:\")

Set colFiles = objFolder.information

For every objFile in colFiles

    OutputFile.Write "C:\," & objFile.identify & "," & objFile.dimension

    Wscript.Echo "C:\," & objFile.identify & "," & objFile.size

next

OutputFile.shut

PowerShell:

Get-ChildItem -route C:\ -File | select-Object directory,identify,size | Export-Csv C:\csv.csv -Append -NoTypeInformation

in case you're nonetheless scripting with batch files or VBscript this facet-through-facet evaluation should have resonated with you.  it would show that:

  • VBscript's return on time identifying the way to automate whatever thing is terrible compared to batch or PowerShell.

  • that you could take what you've discovered scripting with batch data and easily move those competencies over to PowerShell.

  • As script complexity increases, batch, and especially VBscript's code, complexity raises exponentially vs with PowerShell most effective a little.

  • make a decision for your self.  Which language is less complicated to study?  which might be less demanding to use on your everyday tasks? and at last, which language is getting greater aid these days?  when you answer these questions, the language of alternative should still be clear.

    No comments:

    Post a Comment