Wednesday, November 16, 2011

Windows PowerShell : consolidate log files

I'm using Windows PowerShell more and more every day, here's a simple example.

The task is to consolidate several csv files.
With command prompt

copy/b SoftDistribution*.csv Consolidated_logs.csv

With Powershell, use Get-Content and Add-Content

Get-Content SoftDistribution*.csv | Add-Content Consolidated_logs.csv

Now we have used a "text-based" approach in both cases.
In case you have headers in the .csv file, and you just want to filter out some fields of the CSV file, it will get very complicated with the command prompt.
That's when you have to take a more "object-oriented" approach with PowerShell : check out this article from Microsoft's Scripting Guy, which addresses this particular issue.

Since you can use COM and .Net objects in PowerShell, the possibilities are endless! So instead of developing a VBScript for a task we'll run one time only (not a batch), I use PowerShell interactively.