Wednesday, October 19, 2011

Batch process in PowerShell - Get-Content and Import-CSV cmdlets

One of the most important, easy and useful things I see in PowerShell is the possibility to easily process several requests in batch, saving a lot of time. In this article I'm going to explain in this article how to get information from a TXT file and process anything with it using Get-Content cmdlet and also how to get information from a CSV file using the Import-CSV cmdlet.

Get-Content cmdlet:

 PS C:\> Get-Content MyTextFile.txt  
 Line0  
 Line1  
 Line2  
 Line3  
 Line4    

This first example shows us just how to show the file content, however, the most interesting utilization is with a loop, which will allow us to do several tasks in a row for each line of the file as follows (let's say I have 3 computer names on MyTextFile.txt now):

 PS C:\> Get-Content MyTextFile.txt | ForEach { IISRESET $_ }  
 Attempting stop...  
 Internet services successfully stopped  
 Attempting start...  
 Internet services successfully restarted  
 Attempting stop...  
 Internet services successfully stopped  
 Attempting start...  
 Internet services successfully restarted  
 Attempting stop...  
 Internet services successfully stopped  
 Attempting start...  
 Internet services successfully restarted  

You can also limit the number of rows you want to be fetched from the file by using the parameter TotalCount if you want to get the x first records, or using the Select-Object cmdlet with the parameter -Last to have the x last records, as follows:

 PS C:\> Get-Content .\MyTextFile.txt -TotalCount 3  
 Computer0  
 Computer1  
 Computer2  
 PS C:\> Get-Content .\MyTextFile.txt | Select-Object -Last 3  
 Computer7  
 Computer8  
 Computer9  

Import-CSV cmdlet:

Let's say we have a CSV file, an Excel file (which can be easily saved as CSV), or even data exported from any application in comma-separated-values. This can be handled by PowerShell just as easily as the TXT file, plus the possibility to search the records you want to work with by quickly building queries to get information from the file content.

 PS C:\> Import-CSV .\MyCSVFile.csv | Format-Table -AutoSize  
 FullName        Extension CreationTime  LastAccessTime LastWriteTime  
 --------        --------- ------------  -------------- -------------  
 C:\install.res.1028.dll .dll   11/7/2006 9:03 5/6/2011 17:29 11/7/2006 9:03  
 C:\install.res.1031.dll .dll   11/7/2007 9:03 5/6/2011 17:29 11/7/2007 9:03  
 C:\install.res.1033.dll .dll   11/7/2008 9:03 5/6/2011 17:29 11/7/2008 9:03  
 C:\install.res.1036.dll .dll   11/7/2009 9:03 5/6/2011 17:29 11/7/2009 9:03  
 C:\install.res.1040.dll .dll   11/7/2010 9:03 5/6/2011 17:29 11/7/2010 9:03  

First example shows how to just get the file content and using Format-Table cmdlet to have the outcome formatted. Next, let's see how to filter only the ones with date greather than 1/1/2009 by using the Where-Object cmdlet an then again Format-Table for the outcome.

 PS C:\> Import-CSV .\MyCSVFile.csv | Where-Object {[datetime]::Parse($_.CreationTime) -gt '1/1/2009' } | Format-Table -AutoSize  
 FullName        Extension CreationTime  LastAccessTime LastWriteTime  
 --------        --------- ------------  -------------- -------------  
 C:\install.res.1036.dll .dll   11/7/2009 9:03 5/6/2011 17:29 11/7/2009 9:03  
 C:\install.res.1040.dll .dll   11/7/2010 9:03 5/6/2011 17:29 11/7/2010 9:03  

More info:

Get-Content cmdlet
Import-CSV cmdlet

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...