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

  1. Open Windows PowerShell.

  2. Obtain a listing of all the providers by using the Get-PSProvider cmdlet. This is shown here:

    Get-PSProvider

  3. 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:\

  4. 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

  5. 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*"}

  6. 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

  7. 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*"}

  8. 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

  9. 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*"}

  10. 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

  11. 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

Категории