Displaying available command types

05/27/2009

The Get-Command command does not list every command that is available in Windows PowerShell. Instead, the Get-Command command lists only the cmdlets in the current shell. Windows PowerShell actually supports several other types of commands. Aliases, functions, and scripts are also Windows PowerShell commands, although they are not discussed in detail in the Windows PowerShell Primer. External files that are executables, or have a registered file type handler, are also classified as commands.

You can return a listing of all items that can be invoked by entering the following command:

PS> Get-Command *

Because this list includes external files in your search path, it may contain thousands of items. It is more useful to look at a reduced set of commands. To find native commands of other types, you can use the CommandType parameter of the Get-Command cmdlet. Although we have not talked about these other command types yet, you can still display them if you know the name of the CommandType for a class of commands.

Note:

Although we have not discussed it yet, the asterisk (*) is used for wildcard matching in Windows PowerShell command arguments. The * means "match one or more of any characters". You can type Get-Command a* to find all commands that begin with the letter "a". Unlike wildcard matching in Cmd.exe, Windows PowerShell's wildcard will also match a period.

To display the special command category aliases (these are nicknames used as alternatives to standard command names), enter the following command:

PS> Get-Command -CommandType Alias

To display all Windows PowerShell functions, enter the following command:

PS> Get-Command -CommandType Function

To display external scripts in Windows PowerShell's search path, enter the following command:

PS> Get-Command -CommandType ExternalScript

Posted in: Software| Tags: Windows PowerShell Command Display Get external displaying primer detail aliases shell

Getting Summary Command Information

05/27/2009

The Windows PowerShell Get-Command cmdlet retrieves the names of all available commands. When you type Get-Command at a Windows PowerShell prompt, you will see output similar to the following:

PS> Get-Command

CommandType Name Definition

----------- ---- ----------

Cmdlet Add-Content Add-Content [-Path] <String[...

Cmdlet Add-History Add-History [[-InputObject] ...

Cmdlet Add-Member Add-Member [-MemberType] <PS...

...

This output looks a lot like the Help output of Cmd.exe: a tabular summary of internal commands. In the extract of the Get-Command command output shown above, every command shown has a CommandType of Cmdlet. A Cmdlet is Windows PowerShell's intrinsic command type that corresponds roughly to the dir and cd commands of Cmd.exe and to built-ins in UNIX shells such as BASH.

In the output of the Get-Command command, all of the definitions end with ellipses (...) to indicate that PowerShell cannot display all of the content in the available space. When Windows PowerShell displays output, it formats the output as text and then arranges it to make the data fit cleanly into the window. We will talk about this later in the section on formatters.

The Get-Command cmdlet has a Syntax parameter that allows you to retrieve just the syntax of each cmdlet. Enter the Get-Command -Syntax command to display the full output:

PS> Get-Command -Syntax

Add-Content [-Path] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [Credential <PSCredential>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] [-Encoding <FileSystemCmdletProviderEncoding>]

Add-History [[-InputObject] <PSObject[]>] [-Passthru] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <String>][-OutBuffer <Int32>]...

Posted in: Software| Tags: Windows Type PowerShell Command Information Get summary cmdlet output

Cmdlets Use Verb-Noun Names to Reduce Command Memorization

05/27/2009

Windows PowerShell uses a "verb-noun" naming system, where each cmdlet name consists of a standard verb hyphenated with a specific noun. Windows PowerShell verbs are not always English verbs, but they express specific actions in Windows PowerShell. Nouns are very much like nouns in any language, they describe specific types of objects that are important in system administration. It is easy to demonstrate how these two-part names reduce learning effort by looking at a few examples of verbs and nouns.

Nouns are less restricted, but they should always describe what a command acts upon. Windows PowerShell has commands such as Get-Process, Stop-Process, Get-Service, and Stop-Service.

In the case of two nouns and two verbs, consistency does not simplify learning that much. However, if you look at a standard set of 10 verbs and 10 nouns, you then have only 20 words to understand, but those words can be used to form 100 distinct command names.

Frequently, you can recognize what a command does by reading its name, and it is usually apparent what name should be used for a new command. For example, a computer shutdown command might be Stop-Computer. A command that lists all computers on a network might be Get-Computer. The command that gets the system date is Get-Date.

You can list all commands that include a particular verb with the -Verb parameter for Get-Command (We will discuss Get-Command in detail in the next section). For example, to see all cmdlets that use the verb Get, type:

PS> Get-Command -Verb Get

CommandType Name Definition

----------- ---- ----------

Cmdlet Get-Acl Get-Acl [[-Path] <String[]>]...

Cmdlet Get-Alias Get-Alias [[-Name] <String[]...

Cmdlet Get-AuthenticodeSignature Get-AuthenticodeSignature [-...

Cmdlet Get-ChildItem Get-ChildItem [[-Path] <Stri...

...

The -Noun parameter is even more useful because it allows you to see a family of commands that affect the same type of object. For example, if you want to see which commands are available for managing services, type following command:

PS> Get-Command -Noun Service

CommandType Name Definition

----------- ---- ----------

Cmdlet Get-Service Get-Service [[-Name] <String...

Cmdlet New-Service New-Service [-Name] <String>...

Cmdlet Restart-Service Restart-Service [-Name] <Str...

Cmdlet Resume-Service Resume-Service [-Name] <Stri...

Cmdlet Set-Service Set-Service [-Name] <String>...

Cmdlet Start-Service Start-Service [-Name] <Strin...

Cmdlet Stop-Service Stop-Service [-Name] <String...

Cmdlet Suspend-Service Suspend-Service [-Name] <Str...

...

A command is not necessarily a cmdlet, just because it has a verb-noun naming scheme. One example of a native Windows PowerShell command that is not a cmdlet but has a verb-noun name, is the command for clearing a console window, Clear-Host. The Clear-Host command is actually an internal function, as you can see if you run Get-Command against it:

PS> Get-Command -Name Clear-Host

CommandType Name Definition

----------- ---- ----------

Function Clear-Host $spaceType = [System.Managem...

Posted in: Software| Tags: PowerShell Names Verb-Noun Command Script

Learning Windows PowerShell Names

05/27/2009

Learning names of commands and command parameters is a significant time investment with most command-line interfaces. The issue is that there are very few patterns, so the only way to learn is by memorizing each command and each parameter that you need to use on a regular basis.

When you work with a new command or parameter, you cannot generally use what you already know; you have to find and learn a new name. If you look at how interfaces grow from a small set of tools with incremental additions to functionality, it is easy to see why the structure is nonstandard. With command names in particular, this may sound logical since each command is a separate tool, but there is a better way to handle command names.

Most commands are built to manage elements of the operating system or applications, such as services or processes. The commands have a variety of names that may or may not fit into a family. For example, on Windows systems, you can use the net start and net stop commands to start and stop a service. There is another more generalized service control tool for Windows that has a completely different name, sc, that does not fit into the naming pattern for the net service commands. For process management, Windows has the tasklist command to list processes and the taskkill command to kill processes.

Commands that take parameters have irregular parameter specifications. You cannot use the net start command to start a service on a remote computer. The sc command will start a service on a remote computer, but to specify the remote computer, you must prefix its name with a double backslash. For example, to start the spooler service on a remote computer named DC01, you would type sc \\DC01 start spooler. To list tasks running on DC01, you need to use the /S (for "system") parameter and supply the name DC01 without backslashes, like this: tasklist /S DC01.

Although there are important technical distinctions between a service and a process, they are both examples of manageable elements on a computer that have a well-defined life cycle. You may want to start or stop a service or process, or get a list of all currently running services or processes. In other words, although a service and a process are different things, the actions we perform on a service or a process are often conceptually the same. Furthermore, choices we may make to customize an action by specifying parameters may be conceptually similar as well.

Windows PowerShell exploits these similarities to reduce the number of distinct names you need to know to understand and use cmdlets.

Posted in: Software| Tags: Windows Learning PowerShell Names Command way computer service name cannot tool parameter

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions