Powershell lanaguage String Formatting
When we execute a cmdlet we are relying on PowerShell to display results. The â€œFormat-â€ cmdlets allow us to choose which format to display results in. To get a list of formatting options, type the following command:
â€œFormat-Tableâ€ cmdlet displays data in rows and columns, similar to what you would see in an Excel spreadsheet. By default, PowerShell attempts to format the view, but doesnâ€™t always display the best results. For example letâ€™s format the output of the â€œGet-ChildItemâ€ cmdlet for the â€œC:\Windowsâ€ directory.
Get-ChildItem C:\Windows | Format-Table<enter>
I like the table format, but there seems to be too much white space (spaces) between the columns for my taste. Fortunately, there is a parameter -AutoSize to assist us with white space issues.
Get-ChildItem C:\Windows | Format-Table -AutoSize<enter>
O.k. maybe that wasnâ€™t what you were looking for either. As shown from a previous example, use the <tab> key to cycle through the available parameters for the â€œFormat-tableâ€ cmdlet. Expierment with each available parameters to see how format is manipulated.
â€œFormat-Listâ€ cmdlet displays data inâ€¦ of course, a list. For example:
Get-ChildItem C:\Windows | Format-List<enter>
By default, we get the following properties: Name, CreationTime, LastWriteTime, and LastAccessTime. The boss looks at the list and requests the full path to the files. Use the -Property parameter to list the â€œFullNameâ€ property. Reminder: use â€œGet-Memberâ€ cmdlet to list the available properties:
Get-ChildItem C:\Windows | Format-List -Property FullName<enter>
If your boss is like mine, I usually hear something likeâ€¦ â€œthatâ€™s great, but now I want the full path to all files and subdirectories, creation date, and last time a file was modified.â€ Your answerâ€¦ â€œNo Problem!â€ To enumerate multiple properties, just separate each property with a comma (,) as shown in this example:
Get-ChildItem C:\Windows -Recurse | Format-List -Property FullName,CreationTime,LastWriteTime<enter>
Note: You can also use the -Property parameter with the â€œFormat-Tableâ€ cmdlet to build tables, with each property as a header.
â€œFormat-Wideâ€ cmdlet compresses results of a list, to fit the screen:
Get-ChildItem C: | Format-Wide<enter>
The results are in list format sorted by column, similar to the old â€œdir /Dâ€ command. You can customize the number of columns using the -Column number parameter:
Get-ChildItem C: | Format-Wide -Column 3<enter>
The â€œGroup-Objectâ€ cmdlet allows us to format output information based on groups. For example, there is a property call â€œCompanyâ€ for the object returned by the â€œGet-Processâ€ cmdlet. Letâ€™s assign the â€œCompanyâ€ property to a group:
Get-Process | Group-Object Company<enter>
The output groups the information as follows:
Â· Name â€“ Name provided by the Company property of the Get-Process object.
- Count â€“ The number of process running from each Company.
- Group â€“ A truncated list of the processes running.
Here is another example of how to use the â€œGroup-Objectâ€ cmdlet. You want to discover which Event IDs are present in the system log:
Get-EventLog System | Group-Object eventid<enter>
Note: The Name column is the Event ID, even though itâ€™s not labeled as such. By default, â€œGroup-Objectâ€ places the property you specify in the â€œNameâ€ column.
From the example above we are presented with a â€œCountâ€ column. This indicates the number of occurrences per each Event ID. Just as it sounds, the â€œSort-Objectâ€ cmdlet provides a mechanism to sort results. In this example, Iâ€™m going to further build on the command above to sort from most occurrences to least:
Get-EventLog System | Group-Object eventid | Sort-Object Count -descending<enter>
The results are now sorted from most to least number of occurrences. Couple of things to notice here:
Â· I sorted on the â€œCountâ€ column. You could also sort by â€œNameâ€ or any column you choose.
Â· As shown above you can pipe results from one cmdlet to another and then another, etcâ€¦
I love this cmdlet. Itâ€™s not the best html conversion, sometimes the output looks great, sometimes not. Itâ€™s just fast and I donâ€™t have to write any code to convert results, like I did in VBScript. Letâ€™s send the results of â€œGet-Processâ€ to html.
Get-Process | ConvertTo-html<enter>
Well, the cmdlet preformed the conversion but I want the result saved to an html file. Iâ€™m going to use the â€œOut-Fileâ€ cmdlet to redirect the results into a file. This is similar to using the redirect symbol â€œ>â€ in the old dos command shell.
Get-Process | ConvertTo-html | out-file â€œProcesses.htmlâ€<enter>
To open the .html file use the â€œInvoke-Itemâ€ cmdlet. This is equivalent to clicking on a file where the file type determines which application is launched.
The default browser is launched presenting the results saved in the html file. Like I said, doesnâ€™t always look greatâ€¦ But it is fast and the html code can be edited (to look pretty) if desired.
This is another formatting cmdlet that you are going to love. It takes results and converts it to a csv file. In the following examples we are going to Export the processes and open the file in a spreadsheet. You must have Excel or some other type of spreadsheet installed on the system.
1. Export results to a .csv fileâ€
Get-Process | Export-CSV Processes.csv<enter>
2. Open the spreadsheet:
As shown, you can use the â€œExport-CSVâ€ cmdlet to quickly document and/or create reports. Much more simple that what was required to create the same report in VBScript.
Alright, we got through PowerShell Parameters, Objects, and Formatting. We just hit the tip of the iceberg, we will continue utilizing these cmdlet options as we work in later PowerShell tutorials. Obviously, there are more cmdlet options and parameters we have not covered. PowerShell provides the tools (Get-Help and Get-Member) to explore and learn, so snoop around and get your feet wet.
Hope I was able to shed some light on the advantage objects provide over traditional shell â€œtext stringâ€ output. As Objects are the heart of PowerShell and PowerShell scripting, we will be working in depth with objects as we continue our PowerShell training. If you have any comments or questions please use the comment section bellow.