Formatting Basics

PowerShell was designed for use by administrators as opposed to programmers, unlike VBScript. PowerShell knows how to format the objects produced by cmdlets or to display variable and other data typed at the PowerShell prompt. As we have seen in this chapter thus far, you can simply enter a cmdlet or variable at the PowerShell prompt, and get nicely formatted output. You can also use four additional cmdlets, Format-Table, Format-List, Format-Wide, and Format-Custom to create different forms of output.

PowerShell provides you with a number of options for formatting. In most cases, you can just accept PowerShell's built-in formatting. Or, if you are so inclined, you can delve deeper into .NET for richer formatting features.

To illustrate PowerShell formatting, here is a simple command that uses a Get-WMI object to retrieve information about the memory devices in a computer:

PSH [D:\foo]: Get-WMIObject -Class win32_memorydevice _ _GENUS : 2 _ _CLASS : Win32_MemoryDevice _ _SUPERCLASS : Win32_SMBIOSMemory _ _DYNASTY : CIM_ManagedSystemElement _ _RELPATH : Win32_MemoryDevice.DeviceID="Memory Device 0" _ _PROPERTY_COUNT : 39 _ _SERVER : XP21 _ _NAMESPACE : root\cimv2 _ _PATH : \\XP21\root\cimv2:Win32_MemoryDevice.DeviceID="Memory Device 0" Access : AdditionalErrorData : Availability : BlockSize : Caption : Memory Device ConfigManagerErrorCode : ConfigManagerUserConfig : CorrectableError : CreationClassName : Win32_MemoryDevice Description : Memory Device DeviceID : Memory ...

Get Windows Server 2008: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.