Leveraging the Power of Get-Command


Using the Get-Command cmdlet, you can obtain a listing of all the cmdlets installed on the Windows PowerShell, but there is much more that can be done using this extremely versatile cmdlet. One such method of using the Get-Command cmdlet is to use wild card characters.

This is shown in the following procedure:

Just the Steps 

Searching for cmdlets using wild card characters In a Windows Power-Shell prompt, enter the Get-Command cmdlet followed by a wild card character. Example:

 get-command *

Finding commands by using the Get-Command 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 an alias to refer to the Get-Command cmdlet. To find the correct alias, use the Get-Alias cmdlet as follows:

     get-alias g*

  3. This command produces a listing of all the aliases defined that begin with the letter g. An example of the output of this command is shown here:

     CommandType     Name                            Definition  -----------     ----                            ----------  Alias           gal                             Get-Alias  Alias           gc                              Get-Content  Alias           gci                             Get-ChildItem  Alias           gcm                             Get-Command  Alias           gdr                             Get-PSDrive  Alias           ghy                             Get-History  Alias           gi                              Get-Item  Alias           gl                              Get-Location  Alias           gm                              Get-Member  Alias           gp                              Get-ItemProperty Alias           gps                             Get-Process  Alias           group                           Group-Object  Alias           gsv                             Get-Service  Alias           gsnp                            Get-PSSnapin  Alias           gu                              Get-Unique  Alias           gv                              Get-Variable  Alias           gwmi                            Get-WmiObject  Alias           gh                              Get-Help

  4. Using the gcm alias, use the Get-Command cmdlet to return the Get-Command cmdlet. This is shown here:

     gcm get-command

  5. This command returns the Get-Command cmdlet. The output is shown here:

     CommandType     Name                            Definition  -----------     ----                            ----------  Cmdlet          Get-Command                     Get-Command [[-ArgumentList]...

  6. Using the gcm alias to get the Get-Command cmdlet, pipe the output to the Format-List cmdlet. Use the wild card asterisk (*) to obtain a listing of all the properties of the Get-Command cmdlet. This is shown here:

     gcm get-command |format-list *

  7. This command will return all the properties from the Get-Command cmdlet. The output is shown here:

     DLL              : C:\WINDOWS\assembly\GAC_MSIL\System.Management.Automation\1.                    0.0.0__31bf3856ad364e35\System.Management.Automation.dll Verb             : Get Noun             : Command HelpFile         : System.Management.Automation.dll-Help.xml PSSnapIn         : Microsoft.PowerShell.Core ImplementingType : Microsoft.PowerShell.Commands.GetCommandCommand ParameterSets    : {CmdletSet, AllCommandSet} Definition       : Get-Command [[-ArgumentList] <Object[]>] [-Verb <String[]>]                    [-Noun <String[]>] [-PSSnapin <String[]>] [-TotalCount <Int3                    2>] [-Syntax] [-Verbose] [-Debug] [-ErrorAction <ActionPrefe                    rence>] [-ErrorVariable <String>] [-OutVariable <String>] [-                    OutBuffer <Int32>]                    Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>                    ] [-CommandType <CommandTypes>] [-TotalCount <Int32>] [-Synt                    ax] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-                    ErrorVariable <String>] [-OutVariable <String>] [-OutBuffer                    <Int32>] Name             : Get-Command CommandType      : Cmdlet

  8. Using the gcm alias and the Get-Command cmdlet, pipe the output to the Format-List cmdlet. Use the -property argument, and specify the definition property of the Get-Command cmdlet. Rather than retyping the entire command, use the up arrow on your keyboard to retrieve the previous gcm Get-Command | Format-List * command. Use the Backspace key to remove the asterisk and then simply add -property definition to your command. This is shown here:

     gcm get-command | format-list -property definition

  9. This command only returns the property definition for the Get-Command cmdlet. The returned definition is shown here:

     Definition : Get-Command [[-ArgumentList] <Object[]>] [-Verb <String[]>] [-Noun               <String[]>] [-PSSnapin <String[]>] [-TotalCount <Int32>] [-Syntax              ] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVar              iable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]              Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-Co              mmandType <CommandTypes>] [-TotalCount <Int32>] [-Syntax] [-Verbos              e] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <Str              ing>] [-OutVariable <String>] [-OutBuffer <Int32>]

  10. Because objects are returned from cmdlets instead of simply string data, we can also retrieve the definition of the Get-Command cmdlet by directly using the definition property. This is done by putting the expression inside parentheses, and using a “dotted notation,” as shown here:

     (gcm get-command).definition

  11. The definition returned from the previous command is virtually identical to the one returned by using Format-List cmdlet.

  12. Use the gcm alias and specify the -verb argument. Use se* for the verb. This is shown here:

     gcm -verb se*

  13. The previous command returns a listing of all the cmdlets that contain a verb beginning with se. The result is as follows:

     CommandType     Name                            Definition -----------     ----                            ---------- Cmdlet          Select-Object                   Select-Object [[-Property] <... Cmdlet          Select-String                   Select-String [-Pattern] <St... Cmdlet          Set-Acl                         Set-Acl [-Path] <String[]> [... Cmdlet          Set-Alias                       Set-Alias [-Name] <String> [... Cmdlet          Set-AuthenticodeSignature       Set-AuthenticodeSignature [-... Cmdlet          Set-Content                     Set-Content [-Path] <String[... Cmdlet          Set-Date                        Set-Date [-Date] <DateTime> ... Cmdlet          Set-ExecutionPolicy             Set-ExecutionPolicy [-Execut... Cmdlet          Set-Item                        Set-Item [-Path] <String[]> ... Cmdlet          Set-ItemProperty                Set-ItemProperty [-Path] <St... Cmdlet          Set-Location                    Set-Location [[-Path] <Strin... Cmdlet          Set-PSDebug                     Set-PSDebug [-Trace <Int32>]... Cmdlet          Set-Service                     Set-Service [-Name] <String>... Cmdlet          Set-TraceSource                 Set-TraceSource [-Name] <Str... Cmdlet          Set-Variable                    Set-Variable [-Name] <String...

  14. Use the gcm alias and specify the -noun argument. Use o* for the noun. This is shown here:

     gcm -noun o*

  15. The previous command will return all the cmdlets that contain a noun that begins with the letter o. This result is as follows:

     CommandType     Name                            Definition -----------     ----                            ---------- Cmdlet          Compare-Object                  Compare-Object [-ReferenceOb... Cmdlet          ForEach-Object                  ForEach-Object [-Process] <S... Cmdlet          Group-Object                    Group-Object [[-Property] <O... Cmdlet          Measure-Object                  Measure-Object [[-Property] ... Cmdlet          New-Object                      New-Object [-TypeName] <Stri... Cmdlet          Select-Object                   Select-Object [[-Property] <... Cmdlet          Sort-Object                     Sort-Object [[-Property] <Ob... Cmdlet          Tee-Object                      Tee-Object [-FilePath] <Stri... Cmdlet          Where-Object                    Where-Object [-FilterScript]... Cmdlet          Write-Output                    Write-Output [-InputObject] ...

  16. Retrieve only the syntax of the Get-Command cmdlet by specifying the -syntax argument. Use the gcm alias to do this, as shown here:

     gcm -syntax get-command

  17. The syntax of the Get-Command cmdlet is returned by the previous command. The output is as follows:

     Get-Command [[-ArgumentList] <Object[]>] [-Verb <String[]>] [-Noun <String[]>] [-PSSnapin <String[]>] [-TotalCount <Int32>] [-Syntax] [-Verbose] [-Debug] [-Er rorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-CommandType <Co mmandTypes>] [-TotalCount <Int32>] [-Syntax] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <String>] [-OutBuff er <Int32>]

  18. Try to use only aliases to repeat the Get-Command syntax command to retrieve the syntax of the Get-Command cmdlet. This is shown here:

     gcm -syntax gcm

  19. The result of this command is the not the nice syntax description of the previous command. The rather disappointing result is as follows:

     Get-Command

  20. This concludes the procedure for finding commands by using the Get-Command cmdlet.

    image from book
    Quick Check

    Q. To retrieve a definition of the Get-Command cmdlet, using the dotted notation, what command would you use?

    A. (gcm get-command).definition

    image from book




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