Getting Started with PowerShell
上QQ阅读APP看书,第一时间看更新

What can you do?

You saw in the previous chapter that you are able to run standard command-line programs in PowerShell and that there are aliases defined for some cmdlets that allow you to use the names of commands that you are used to from other shells. Other than these, what can you use? How do you know which commands, cmdlets, and aliases are available?

The answer is the first of the big three cmdlets, the Get-Command cmdlet. Simply executing Get-Command with no parameters displays a list of all the entities that PowerShell considers to be executable. This includes programs in the path (the environment variable), cmdlets, functions, scripts, and aliases.

What can you do?

This list of commands is long and gets longer with each new operating system and PowerShell release. To count the output, we can use this command:

Get-Command | Measure-Object

The output of this command is as follows:

What can you do?

Tip

The pipe character (|) tells PowerShell to use the output of the command on the left of the pipe as the input of the command on the right. In this example, it says that the output of the get-command cmdlet should be used as the input for the measure-object cmdlet.

The Measure-Object cmdlet can be used to count the number of items in the output from a command. In this case, it tells us that there are 1444 different commands available to us. In the output, we can see that there is a CommandType column. There is a corresponding parameter to the Get-Command cmdlet that allows us to filter the output and only shows us certain kinds of commands. Limiting the output to the cmdlets shows that there are 706 different cmdlets available:

What can you do?

Getting a big list such as this isn't very useful though. In reality, you are usually going to use Get-Command to find commands that pertain to a component you want to work with. For instance, if you need to work with Windows services, you could issue a command such as Get-Command *Service* using wildcards to include any command that includes the word service in its name. The output would look something similar to the following:

What can you do?

You can see in the output that applications found in the current path and the PowerShell cmdlets are both included. Unfortunately, there's also a cmdlet called New-WebServiceProxy, which clearly doesn't have anything to do with Windows services. To better control the matching of cmdlets with your subject, you can use the –Noun parameter to filter the commands. Note that by naming a noun, you are implicitly limiting the results to PowerShell objects because nouns are a PowerShell concept. Using this approach gives us a smaller list:

What can you do?