Understanding the Basics of Cmdlets


In Chapter 1, Overview of Windows PowerShell, we learned about using the various help utilities available that demonstrate how to use cmdlets. We looked at a couple of cmdlets that are helpful in finding out what commands are available and how to obtain information about them. In this section, we describe some additional ways to use cmdlets in Windows PowerShell.

Tip 

Typing long cmdlet names can be somewhat tedious. To simplify this process, type enough of the cmdlet name to uniquely distinguish it, and then press the Tab key on the keyboard. What is the result? Tab Completion completes the cmdlet name for you. This also works with argument names and other things you are entering. Feel free to experiment with this great time-saving technique. You may never have to type get-command again!

Because the cmdlets return objects instead of “string values,” we can obtain additional information about the returned objects. The additional information would not be available to us if we were working with just string data. To do this, we can use the pipe character (|) to take information from one cmdlet and feed it to another cmdlet. This may seem complicated, but it is actually quite simple and, by the end of this chapter, will seem quite natural. At the most basic level, consider obtaining a directory listing; after you have the directory listing, perhaps you would like to format the way it is displayed-as a table or a list. As you can see, these are two separate operations: obtaining the directory listing, and formatting the list. The second task will take place on the right side of the pipe.

Using the Get-ChildItem Cmdlet

In Chapter 1, we used the dir command to obtain a listing of all the files in a directory. This works because there is an alias built into Windows PowerShell that assigns the Get-ChildItem cmdlet to the letter combination dir.

Just the Steps 

Obtaining a directory listing In a Windows PowerShell prompt, enter the Get-ChildItem cmdlet followed by the directory to list. Example:

 get-childitem C:\

In Windows PowerShell, there actually is no cmdlet called dir, nor does it actually use the dir command. The alias dir is associated with the Get-ChildItem cmdlet. This is why the output from dir is different in Windows PowerShell than in the CMD.exe interpreter. The alias dir is used when we use the Get-Alias cmdlet to resolve the association, as follows:

 PS C:\> get-alias dir    CommandType     Name                            Definition  -----------     ----                            ----------  Alias           dir                             Get-ChildItem

If you use the Get-ChildItem cmdlet to obtain the directory listing, it will obtain a listing the same as dir because dir is simply an alias for Get-ChildItem. This is shown here:

 PS C:\> get-childitem C:\     Directory: Microsoft.PowerShell.Core\FileSystem::C:\ Mode                LastWriteTime      Length Name  ----                -------------     ------ ----  d----          7/2/2006   3:14 PM             audioBOOK  d----         11/4/2006   4:57 AM             Documents and Settings  d----          2/6/2006   4:49 PM             DsoFile  d----          9/5/2006   2:30 PM             fso  d----        11/30/2006   2:08 PM             fso1  d----         7/21/2006   6:08 AM             fso2  d----         12/2/2005   5:41 AM             German  d----         9/24/2006   1:54 AM             music  d----        12/10/2006   6:54 AM             mytest  d----        12/13/2006   8:30 AM             OutlookMail  d-r--        11/20/2006   6:44 PM             Program Files  d----         7/16/2005   2:52 PM             RAS  d----         1/30/2006  11:30 AM             smartPhone  d----         11/2/2006   1:35 AM             Temp  d----         8/31/2006   9:48 AM             Utils  d----         1/30/2006  11:10 AM             vb05sbs  d----         12/5/2006   8:01 AM             WINDOWS  -a---         12/8/2006   7:24 PM       22950 a.txt  -a---         12/5/2006   8:48 AM       23902 alias.txt  -a---         7/16/2005   1:39 PM           0 AUTOEXEC.BAT  -a---         11/7/2006   3:09 PM        3988 bar.emf  --r-s         8/27/2006   9:37 PM         211 boot.ini  -a---         12/3/2006   7:36 AM       21228 cmdlets.txt  -a---        12/13/2006   9:44 AM      273612 commandHelp.txt  -a---        12/10/2006   7:34 AM       21228 commands.txt  -a---         7/16/2005   1:39 PM           0 CONFIG.SYS  -a---         12/7/2006   3:14 PM        8261 mySheet.xls  -a---         12/7/2006   5:29 PM        2960 NetDiag.log  -a---         12/5/2006   8:29 AM       16386 notepad  -a---          6/3/2006   2:11 PM         102 Platform.ini  -a---         12/7/2006   5:29 PM       10670 tshoot.txt  -a---         12/4/2006   9:09 PM       52124 VistaResKitScripts.txt 

If you were to use Get-Help and then dir, you would receive the same output as if you were to use Get-Help Get-ChildItem. In Windows PowerShell, the two can be used in exactly the same fashion.

Just the Steps 

Formatting a directory listing using Format-List In a Windows PowerShell prompt, enter the Get-ChildItem cmdlet followed by the directory to list followed by the pipe character and the Format-List cmdlet. Example:

 get-childitem C:\ | format-list

Formatting output with the Format-List cmdlet

  1. Start Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Use the Get-ChildItem cmdlet to obtain a directory listing of the C:\ directory.

     get-childItem C:\

  3. Use the Format-List cmdlet to arrange the output of Get-ChildItem.

     get-childitem |format-list

  4. Use the -property argument of the Format-List cmdlet to retrieve only a listing of the name of each file in the root.

     get-childitem C:\ | format-list -property name

  5. Use the property argument of the Format-List cmdlet to retrieve only a listing of the name and length of each file in the root.

     get-childitem C:\ | format-list -property name, length

Using the Format-Wide Cmdlet

In the same way that we use the Format-List cmdlet to produce an output in a list, we can use the Format-Wide cmdlet to produce a more compact output.

Just the Steps 

Formatting a directory listing using Format-Wide In a Windows PowerShell prompt, enter the Get-ChildItem cmdlet followed by the directory to list followed by the pipe character and the Format-Wide cmdlet. Example:

 get-childitem C:\ | format-wide

Formatting output with the Format-Wide cmdlet

  1. Start Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Use the Get-ChildItem cmdlet to obtain a directory listing of the C:\Windows directory.

     get-childitem C:\Windows

  3. Use the -recursive argument to cause the Get-ChildItem cmdlet to walk through a nested directory structure, including only .txt files in the output.

     get-childitem C:\Windows -recurse -include *.txt

  4. A partial output from the command is shown here:

             Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Driver Cache  Mode                LastWriteTime      Length Name  ----                -------------     ------ ----  -a---        11/26/2004   6:29 AM       13512 yk51x86.txt            Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Help\Tours\mmTo          ur    Mode                LastWriteTime      Length Name  ----                -------------     ------ ----  -a---          8/4/2004   8:00 AM        807 intro.txt  -a---          8/4/2004   8:00 AM        407 nav.txt  -a---          8/4/2004   8:00 AM        747 segment1.txt  -a---          8/4/2004   8:00 AM        772 segment2.txt  -a---          8/4/2004   8:00 AM        717 segment3.txt  -a---          8/4/2004   8:00 AM        633 segment4.txt  -a---          8/4/2004   8:00 AM        799 segment5.txt

  5. Use the Format-Wide cmdlet to adjust the output from the Get-ChildItem cmdlet. Use the -columns argument and supply a parameter of 3 to it. This is shown here:

     get-childitem C:\Windows -recurse -include *.txt |format-wide -column 3

  6. Once this command is run, you will see an output similar to this:

         Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Driver Cache yk51x86.txt     Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Help\Tours\mmTo     ur intro.txt                  nav.txt                    segment1.txt segment2.txt               segment3.txt               segment4.txt segment5.txt     Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Microsoft.NET\F     ramework\v1.1.4322\1033 SetupENU1.txt              SetupENU2.txt     Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Microsoft.NET\F     ramework\v2.0.50727\Microsoft .NET Framework 2.0 eula.1025.txt              eula.1028.txt              eula.1029.txt eula.1030.txt              eula.1031.txt              eula.1032.txt eula.1033.txt              eula.1035.txt              eula.1036.txt eula.1037.txt              eula.1038.txt              eula.1040.txt eula.1041.txt              eula.1042.txt              eula.1043.txt eula.1044.txt              eula.1045.txt              eula.1046.txt eula.1049.txt              eula.1053.txt              eula.1055.txt eula.2052.txt              eula.2070.txt              eula.3076.txt eula.3082.txt

  7. Use the Format-Wide cmdlet to adjust the output from the Get-ChildItem cmdlet. Use the property argument to specify the name property, and group the outputs by size. The command shown here appears on two lines; however, when typed into Windows PowerShell, it is a single command and needs to be on the same line:

     get-childitem C:\Windows -recurse -include *.txt |format-wide -property name -groupby length -column 3

  8. A partial output is shown here. Note that although three columns were specified, if there are not three files of the same length, only one column will be used:

        Length: 13512 yk51x86.txt    Length: 807 intro.txt    Length: 407 nav.txt    Length: 747 segment1.txt

Just the Steps 

Formatting a directory listing using Format-Table In a Windows PowerShell prompt, enter the Get-ChildItem cmdlet followed by the directory to list followed by the pipe character and the Format-Table cmdlet. Example:

 get-childitem C:\ | format-table

Formatting output with the Format-Table cmdlet

  1. Start the Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Use the Get-ChildItem cmdlet to obtain a directory listing of the C:\Windows directory

     get-childitem C:\Windows

  3. Use the -recursive argument to cause the Get-ChildItem cmdlet to walk through a nested directory structure, include only .txt files in the output.

     get-childitem C:\Windows -recurse -include *.txt

  4. Use the Format-Table cmdlet to adjust the output from the Get-ChildItem cmdlet. This is shown here:

     get-childitem C:\Windows -recurse -include *.txt |format-table

  5. The command results in the creation of a table, as follows:

             Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Driver Cache    Mode                LastWriteTime      Length Name  ----                -------------     ------ ----  -a---        11/26/2004   6:29 AM      13512 yk51x86.txt         Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Help\Tours\mmTo      ur    Mode                LastWriteTime      Length Name  ----                -------------     ------ ----  -a---          8/4/2004   8:00 AM         807 intro.txt  -a---          8/4/2004   8:00 AM         407 nav.txt  -a---          8/4/2004   8:00 AM         747 segment1.txt  -a---          8/4/2004   8:00 AM         772 segment2.txt  -a---          8/4/2004   8:00 AM         717 segment3.txt  -a---          8/4/2004   8:00 AM         633 segment4.txt  -a---          8/4/2004   8:00 AM         799 segment5.txt         Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\Microsoft.NET\F      ramework\v1.1.4322\1033   Mode                LastWriteTime     Length Name  ----                -------------     ------ ----  -a---          3/6/2002   2:36 PM         38 SetupENU1.txt  -a---          3/6/2002   2:36 PM         38 SetupENU2.txt

  6. Use the -property argument of the Format-Table cmdlet and choose the name, length, and last-write-time properties. This is shown here:

     get-childitem C:\Windows -recurse -include *.txt |format-table -property  name, length, lastwritetime

  7. This command results in producing a table with the name, length, and last write time as column headers. A sample of this output is shown here:

     Name                                           Length LastWriteTime  ----                                           ------ -------------  yk51x86.txt                                     13512 11/26/2004 6:29:00 AM  intro.txt                                         807 8/4/2004 8:00:00 AM  nav.txt                                           407 8/4/2004 8:00:00 AM  segment1.txt                                      747 8/4/2004 8:00:00 AM  segment2.txt                                      772 8/4/2004 8:00:00 AM  segment3.txt                                      717 8/4/2004 8:00:00 AM  segment4.txt                                      633 8/4/2004 8:00:00 AM 




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net