MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
In Chapter 1, Overview of Windows PowerShell, we presented the various Help utilities available that show how to use cmdlets. The alias provider provides easy-to-use access to all aliases defined in Windows PowerShell. To work with the aliases on your machine, use the Set-Location cmdlet and specify the Alias:\ drive. You can then use the same cmdlets you would use to work with the file system.
Tip | With the alias provider, you can use a Where-Object cmdlet and filter to search for an alias by name or description. |
Working with the alias provider
-
Open Windows PowerShell.
-
Obtain a listing of all the providers by using the Get-PSProvider cmdlet. This is shown here:
Get-PSProvider
-
The PSDrive associated with the alias provider is called Alias. This is seen in the listing produced by the Get-PSProvider cmdlet. Use the Set-Location cmdlet to change to the Alias drive. Use the sl alias to reduce typing. This command is shown here:
sl alias:\
-
Use the Get-ChildItem cmdlet to produce a listing of all the aliases that are defined on the system. To reduce typing, use the alias gci in place of Get-ChildItem. This is shown here:
GCI
-
Use a Where-Object cmdlet filter to reduce the amount of information that is returned by using the Get-ChildItem cmdlet. Produce a listing of all the aliases that begin with the letter s. This is shown here:
GCI | Where-Object {$_.name -like "s*"}
-
To identify other properties that could be used in the filter, pipeline the results of the Get-ChildItem cmdlet into the Get-Member cmdlet. This is shown here:
Get-ChildItem |Get-Member
-
Press the up arrow twice, and edit the previous filter to include only definitions that contain the word set. The modified filter is shown here:
GCI | Where-Object {$_.definition -like "set*"}
-
The results of this command are shown here:
CommandType Name Definition ----------- ---- ---------- Alias sal Set-Alias Alias sc Set-Content Alias si Set-Item Alias sl Set-Location Alias sp Set-ItemProperty Alias sv Set-Variable Alias cd Set-Location Alias chdir Set-Location Alias set Set-Variable
-
Press the up arrow three times, and edit the previous filter to include only names of aliases that are like the letter w. This revised command is seen here:
GCI | Where-Object {$_.name -like "*w*"}
-
The results from this command are similar to those shown here:
CommandType Name Definition ----------- ---- ---------- Alias fw Format-Wide Alias gwmi Get-WmiObject Alias where Where-Object Alias write Write-Output Alias pwd Get-Location
-
From the list above, note that where is an alias for the Where-Object cmdlet. Press the up arrow one time to retrieve the previous command. Edit it to use the where alias instead of spelling out the entire Where-Object cmdlet name. This revised command is seen here:
GCI | where {$_.name -like "*w*"}
Caution When using the Set-Location cmdlet to switch to a newly created PSDrive, you must follow the name of the PSDrive with a colon. A trailing forward slash or backward slash is optional. An error will be generated if the colon is left out, as shown in Figure 3-1. I prefer to use the backward slash (\) because it is consistent with normal Windows file system operations.
Figure 3-1: Using Set-Location without : results in an error
Категории