Another one-liner in Windows PowerShell

I did mention that I have been trying to convert my VBScript files to PowerShell scripts due to ease of coding. One of them happened to be a script that iterates a folder containing logs, reads the contents of the log files and appends them to a text file (I was actually trying to consolidate all the RADIUS logs for parsing and storage in a database). In VBScript, here’s how it is written

Const ForReading = 1
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set folder = objFSO.GetFolder(“d:a”)
Set outfile = objFSO.CreateTextFile(“d:testout.txt”)

for each file in folder.Files
Set testfile = objFSO.OpenTextFile(file.path, ForReading)
Do While Not testfile.AtEndOfStream

line=testfile.ReadLine
‘write to a single output file
outfile.writeline(line)
Loop
testfile.close
Next

outfile.close
Set objFSO = Nothing

Set folder = Nothing
Set outfile = Nothing

Here’s how you can do it in PowerShell – with the cmdlet aliases


ls d:a gc ac d:testout.txt

This should be more than enough reason to learn PowerShell as a system administrator. More to come

Comments

comments

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 thoughts on “Another one-liner in Windows PowerShell

  1. Love this. This is a great example of why all Microsoft Administrators, Techs, whatever should learn powershell. You can do so much with it and the power of it just keeps growing. Great post and great example of how powershell makes simple tasks simple again.

  2. Thanks for visiting my blog. I keep hearing comments about how Windows professionals make a living by using only their two fingers – for doing left- and right-clicks on the mouse. The real return on investment in managing systems and software is when one no longer has to use the mouse at all and decrease administration overhead. That's the value that scripting (and that is not just limited to PowerShell) provides to any IT professional. PowerShell just made it easier and simpler.Let me know how else can I be of service