Sunday, February 28, 2010

Discovery In PowerShell – Get-Help vs. Get-Command

Discovery is process by which a user will learn to use any product. The more discoverable any product is, the easier it is for new users to get started. A disoverable product enables any user both to leverage what they already know and to find more. PowerShell was designed to be, and I believe is, a discoverable product.

One example of this came up this week – looking at how you can find cmdlets (and other stuff) that you can run. By using Get-Help, Get-Command and some where clause foo, you can quickly locate and learn about cmdlets in your environment. And, this help information provides examples to illustrate the use of a particular cmdlet and some products (eg SCVMM) add additional About_* files.

Get-Command and Get-Help are similar but different. They both can be used to find cmdlets and other help information. So while sharing a similar usage, they return different information. Get-Help returns information on 4 categories: Alias, Cmdlet, Provider and HelpFile. Get-Command, on the other hand, returns information of 6 different command types: Alias, Application, Function, Cmdlet, ExternalScript and Filter.

Where this helps is knowing which cmdlet to use to search for what. If you are using pure PowerShell Cmdlets, modules and scripts, Get-Help will be your main discovery tool. But if you are also using console applications from, say earlier versions of SharePoint, then Get-Command will help to find the commands you might need.

The trick to getting the most out of these cmdlets is knowing how to search and the key to that is using wildcards. If you want to find out what cmdlets, etc, are available for a PSSession object,

 

PSH [C:\]: Get-Help *PSSEssion*

Name                              Category  Synopsis
----                              --------  --------
Register-PSSessionConfiguration   Cmdlet    Creates and registers a new session configuration.
Unregister-PSSessionConfiguration Cmdlet    Deletes registered session configurations from the computer.
Get-PSSessionConfiguration        Cmdlet    Gets the registered session configurations on the computer.
Set-PSSessionConfiguration        Cmdlet    Changes the properties of a registered session configuration.
Enable-PSSessionConfiguration     Cmdlet    Enables the session configurations on the local computer.
Disable-PSSessionConfiguration    Cmdlet    Denies access to the session configurations on the local computer.
New-PSSession                     Cmdlet    Creates a persistent connection to a local or remote computer.
Get-PSSession                     Cmdlet    Gets the Windows PowerShell sessions (PSSessions) in the current session.
Remove-PSSession                  Cmdlet    Closes one or more Windows PowerShell sessions (PSSessions).
Enter-PSSession                   Cmdlet    Starts an interactive session with a remote computer.
Exit-PSSession                    Cmdlet    Ends an interactive session with a remote computer.
New-PSSessionOption               Cmdlet    Creates an object that contains advanced options for a PSSession.
Export-PSSession                  Cmdlet    Imports commands from another session and saves them in a Windows PowerS...
Import-PSSession                  Cmdlet    Imports commands from another session into the current session.
about_pssessions                  HelpFile  Describes Windows PowerShell sessions (PSSessions) and explains how to
about_pssession_details           HelpFile  Provides detailed information about Windows PowerShell sessions and the

Or try the following:

PSH [C:\]: get-command *PSSession*

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Disable-PSSessionConfiguration                      Disable-PSSessionConfiguration [[-Name] <String[...
Cmdlet          Enable-PSSessionConfiguration                       Enable-PSSessionConfiguration [[-Name] <String[]...
Cmdlet          Enter-PSSession                                     Enter-PSSession [-ComputerName] <String> [-Crede...
Cmdlet          Exit-PSSession                                      Exit-PSSession [-Verbose] [-Debug] [-ErrorAction...
Cmdlet          Export-PSSession                                    Export-PSSession [-Session] <PSSession> [-Output...
Cmdlet          Get-PSSession                                       Get-PSSession [[-ComputerName] <String[]>] [-Ver...
Cmdlet          Get-PSSessionConfiguration                          Get-PSSessionConfiguration [[-Name] <String[]>] ...
Cmdlet          Import-PSSession                                    Import-PSSession [-Session] <PSSession> [[-Comma...
Cmdlet          New-PSSession                                       New-PSSession [[-ComputerName] <String[]>] [-Cre...
Cmdlet          New-PSSessionOption                                 New-PSSessionOption [-MaximumRedirection <Int32>...
Cmdlet          Register-PSSessionConfiguration                     Register-PSSessionConfiguration [-Name] <String>...
Cmdlet          Remove-PSSession                                    Remove-PSSession [-Id] <Int32[]> [-Verbose] [-De...
Cmdlet          Set-PSSessionConfiguration                          Set-PSSessionConfiguration [-Name] <String> [-Ap...
Cmdlet          Unregister-PSSessionConfiguration                   Unregister-PSSessionConfiguration [-Name] <Strin...

If you are looking for more conceptual information about some feature (typically the noun in the cmdlet name – as here “PSSession”) Get-Help will return the ‘About_’ topics, These help you to learn more about the underlying objects, as well as the help information about specific cmdlets. I personally use Get-Help to remind myself of the cmdlets names and spellings.

And finally – if you are using Get-Help on your local machine, you can usually specify the –online parameter. This opens up a browser window into Technet, pointing to the current documentation. If you look at the online help for Enter-PSSession (http://technet.microsoft.com/en-us/library/dd315384.aspx), you can see this page was updated mid December. The PowerShell writers do a fantastic job of fixing the online versions of the help. And they are working on a way to get these updates to you as fast as possible.

1 comment:

jv said...

Try:
get-command|sort commandtype,name |more

Great index of all things PowerShell