Top 10 Tips for Using Windows PowerShell
Pages: 1, 2, 3, 4, 5
7. Format your output
You have full control of how the format looks of your output using the various Format- commands.
PowerShell shuttles objects through the pipeline and only obtains the text version of the objects when ready for display, unless told to do otherwise with the commands. If you look at the list of members in the objects that come out of Get-Process, however, you'll see far more than what you see in the output. Which members appear in the output? The answer comes from the system configuration. Inside the Windows PowerShell directory is a file called dotnettypes.format.ps1xml. This is an XML file that describes the formatting to be used in the output of various types. There are actually several such files, each describing different types, each with the filename pattern *.format.ps1xml. Others include powershellcore.format.ps1xml and filesystem.format.ps1xml. You can learn more about these files here.
To format the output, PowerShell users a command called FormatTable. This command takes a pipeline of objects and writes them out using the format defined as the *.format.ps1xml files.
You can, however, override the settings and choose which members appear in the output, like so:
Get-Process | Format-Table Id, Name
Id Name
-- ----
2876 alg
532 ApntEx
2044 Apoint
3448 calc
1824 CFSvcs
2176 cmd
3760 cmd
1640 Crypserv
1316 csrss
This will display the Id and Name columns in the table, regardless of the default settings.
PowerShell has four different Format commands:
Format-CustomFormat-ListFormat-TableFormat-Wide
You can explore each of these with Get-Help.
8. Remember everything is a hierarchy
PowerShell Drives exist for more than just the file system. You can access the registry, the environment, and other data as a drive.
The idea of a computer being arranged hierarchically has been around a long time. Unix has always treated all the hardware as being part of the hierarchical filesystem, for example. Similarly, PowerShell treats various aspects of the computer as drives. You can see what all drives are currently available with the Get-PSDrive command:
PS C:\Documents and Settings\Jeff> Get-PSDrive
Name Provider Root CurrentLocation
---- -------- ---- ---------------
Alias Alias
C FileSystem C:\ Documents and Settings\Jeff
cert Certificate \
D FileSystem D:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Variable Variable
The list of aliases, for example, is a drive. You can switch to that drive as you would any other drive like so:
PS C:\WINDOWS\system32> cd alias:
PS Alias:\>
PS Alias:\> dir
CommandType Name Definition
----------- ---- ----------
Alias ac Add-Content
Alias asnp Add-PSSnapin
Alias clc Clear-Content
Alias cli Clear-Item
Alias clp Clear-ItemProperty
Alias clv Clear-Variable
The alias: drive doesn't provide subdirectories. Others, however, such as the Registry, do:
PS Alias:\> cd HKCU:
PS HKCU:\> dir
Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER
SKC VC Name Property
--- -- ---- --------
2 0 AppEvents {}
1 0 CLSID {}
4 32 Console {ColorTable00, ColorTable01, ColorTab...
24 1 Control Panel {Opened}
0 8 Environment {BAKEFILE_PATHS, INCLUDE, LIB, PROMPT...
0 1 HBA {Version}
1 5 Identities {Identity Ordinal, Migrated5, Last Us...
3 0 Keyboard Layout {}
0 0 Movie Magic Screenwriter {}
19 0 Movie Magic Screenwriter Vo... {}
0 0 Network {}
1 1 Note-It {(default)}
4 1 Printers {DeviceOld}
1 1 RemoteAccess {InternetProfile}
1 7 S {AutodiscoveryFlags, DetectedInterfac...
100 1 Software {(default)}
0 0 UNICODE Program Groups {}
2 0 Windows 3.1 Migration Status {}
1 0 Win_32 {}
0 1 SessionInformation {ProgramCount}
0 7 Volatile Environment {LOGONSERVER, CLIENTNAME, SESSIONNAME...
PS HKCU:\> cd Software\Microsoft
PS HKCU:\Software\Microsoft> dir
Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microso
ft
SKC VC Name Property
--- -- ---- --------
1 0 Active Setup {}
3 0 ActiveMovie {}
2 0 Advanced INF Setup {}
1 0 ASF Stream Descriptor File {}
1 0 Automap {}
1 4 Broadband Networking {StatusTimeout, InternetStatusTimeout...
1 1 ClipArt Gallery {UserAdded}
0 5 Clipbook {WindowsClipBook Viewer, WindowsClipb...
0 2 Clock {iFormat, {CCF5A555-D92E-457b-9235-2B...
Just as you could in DOS, you can use filename patterns:
PS HKCU:\Software\Microsoft> dir *windows*
Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microso
ft
SKC VC Name Property
--- -- ---- --------
3 0 Windows {}
0 1 Windows Genuine Advantage {code}
0 5 Windows Help {Maximized, Xl, Xr, Yd...}
1 0 Windows Media {}
1 0 Windows NT {}
1 0 Windows Script {}
1 0 Windows Script Host {}
The ? character will match a single character. For example, abc? will match all items starting with abc followed by a single character. You can also use brackets to specify a set of characters to match. For example, abc[123] will match abc1, abc2, and abc3.

