Thursday, March 15, 2012

Month of Lunches - Day 8

Today's chapter is over "Formatting, and why its done on the right". I cannot put this any simplier than Don did for the title of section 8.1 "Formatting: Making what you see Prettier". This Chapter is all about how to make those outputs a little more visually appealing and displaying the proper information where you want it.

The first thing that Don covers is the default formatting and how it works. In a nut shell, the formatting is done by XML files. These are labeled with a .Format.ps1XML. You can look find these files in the PowerShell installation directory.

VERY IMPORTANT: Do not modify these files in any way. If you open them never save any changes as they are digitally signed files and modifying them will break the signature, after which PowerShell is unable to use them. So that default formatting that you get on outputs, you will not get again.

Ok, so you want to see what is there? Open up your PowerShell console and follow along.

                CD $PSHome - This Will bring you to your installation folder

Lets just search for all of them to see what we have.

                DIR *format.ps1xml

                Certificate.format.ps1xml
                Diagnostics.Format.ps1xml
                DotNetTypes.format.ps1xml
                Event.Format.ps1xml
                FileSystem.format.ps1xml
                Help.format.ps1xml
                HelpV3.format.ps1xml
                PowerShellCore.format.ps1xml
                PowerShellTrace.format.ps1xml
                Registry.format.ps1xml
                WSMan.Format.ps1xml

You can see by my outputs here that I have installed PowerShell V3 on my own PC where I looked this up, but in V2 they should be very similar. Well lets open one up and take a look shall we.

                Notepad DotNetTypes.format.ps1xml

This will launch your notepad and you should see the xml file displayed. You can look up specific format layouts by there complete type name with the handy dandy, find window. Sorry about that, to much Blues Clues with my daughter. Anyway here you will see the way that object outputs are formated including the column header names (by property), Width, alignment in the table, and various other pieces of information. Basically how it is explained that this works is when you type a command, say Get-Process, the cmdlet goes out and gathers its data, and places that particular data object into the pipeline. As was explained to us before every command has an OUT- command following it even if you didn’t put one. It this case it is Out-Default, which passes the object to Out-Host. These out cmdlets are designed to work with the format.ps1xml file to format your data.

We also covered a number of Format- commands. The first of which was Format-Table. This cmdlet allows you to specify parameters to custom fit the table to your screen (-Autosize), Specify the particular properties to be displayed in the table (-Property), Group your data by a particular property value (-GroupBy), or even Wrap the text if some of the property fields are to long (-Wrap big surprise I bet). Are you starting to get the idea that they tried to dummy proof the cmdlets and there parameters.

Format-List and Format-Wide do exactly as they state. They are both used for Formatting lists which is good for data that you have a lot property values that you need to see. It just groups them by object as seen below with Get-Process | Format-List

                Id      : 432
                Handles : 149
                CPU     : 2.515625
                Name    : explorer

The last thing that few covered was another way to output data. Out-Gridview. In order to use this you must have the PowerShell IDE installed (which it is not by default). This displays a GUI table, similar to what you would see in Excel, that allows you to sort, rearrange columns, remove\add properties fields, or filter data. Super simple and great if you just want to pull something up and look at it. Well im way over my 500 words for the day but this chapter was well worth it.

No comments:

Post a Comment