Friday, January 27, 2012

Powershell Formatting

Would you like to customize the output from powershell commands?  You can easily do this in powershell itself, no need to move the output to a unix shell for properly formatting your data.  
Example:
Getting information about currently running processes.  The standard output for the Get-Process cmdlet looks like this
PS C:\Users\name> Get-Process ie*
But there are MANY other attributes available from this command.  To learn what the other attributes are we append ‘| Format-List *’ to the command line and get a long listing returned (partial list shown).
PS C:\Users\name> Get-Process ie* | Format-List *
For our report, we only want to return the name, id and PeakPagedMemorySize.  We can do so with the following command.
Get-Process ie* | Format-List name, id, Responding, PeakPagedMemorySize
But, this listing isn’t very user friendly.  It would require additional formatting to put this data into excel for further processing.  So, we use Format-Table to change the view into a table.
Get-Process ie* | Format-Table name, id, Responding, PeakPagedMemorySize
But, the entire window to display the data.  Also, if we displaying several fields it will word wrap each line.  So, we introduce one more sorting/presenting option for Format-Table, -AutoSize and we are presented with a nicely sorted list.
Get-Process ie* | Format-Table name, id, Responding, PeakPagedMemorySize -AutoSize 




No comments:

Post a Comment