Understanding the Function Provider


The Function provider provides access to the functions defined in Windows PowerShell. By using the function provider you can obtain a listing of all the functions on your system. You can also add, modify, and delete functions. The function provider uses a file system–based model, and the cmdlets learned earlier also apply to working with functions. The commands used in the procedure are in the image from book ListingAllFunctionsOnTheSystem.txt file.

Listing all functions on the system

  1. Open Windows PowerShell.

  2. Use the Set-Location cmdlet to change the working location to the function PSDrive. This command is shown here:

     Set-Location function:\

  3. Use the Get-ChildItem cmdlet to enumerate all the functions. Do this by using the gci alias, as shown here:

     GCI

  4. The resulting list contains many functions that use Set-Location to the different drive letters. A partial view of this output is shown here:

     CommandType     Name                            Definition -----------     ----                            ---------- Function        prompt                          'PS ' + $(Get-Location) + $(... Function        TabExpansion                    ... Function        Clear-Host                      $spaceType = [System.Managem... Function        more                            param([string[]]$paths);  if... Function        help                            param([string]$Name,[string[... Function        man                             param([string]$Name,[string[... Function        mkdir                           param([string[]]$paths); New... Function        md                              param([string[]]$paths); New... Function        A:                              Set-Location A: Function        B:                              Set-Location B: Function        C:                              Set-Location C: Function        D:                              Set-Location D:

  5. To return only the functions that are used for drives, use the Get-ChildItem cmdlet and pipe the object returned into a Where-Object cmdlet. Use the default $_ variable to filter on the definition attribute. Use the like argument to search for definitions that contain the word set. The resulting command is shown here:

     GCI | Where {$_.definition -like "set*"}

  6. If you are more interested in functions that are not related to drive mappings, then you can use the notlike argument instead of like. The easiest way to make this change is to use the up arrow and retrieve the gci | where {$_.definition -like "set*"} and then change the filter from like to notlike. The resulting command is shown here:

     GCI | Where {$_.definition -notlike "set*"}

  7. The resulting listing of functions is shown here:

     CommandType     Name                            Definition -----------     ----                            ---------- Function        prompt                          'PS' + $(Get-Location) + $(... Function        TabExpansion                    ... Function        Clear-Host                      $spaceType = [System.Managem... Function        more                            param([string[]]$paths);  if... Function        help                            param([string]$Name,[string[... Function        man                             param([string]$Name,[string[... Function        mkdir                           param([string[]]$paths); New... Function        md                              param([string[]]$paths); New... Function        pro                             notepad $profile

  8. Use the Get-Content cmdlet to retrieve the text of the md function. This is shown here:

     Get-Content md

  9. The content of the md function is shown here:

     param([string[]]$paths); New-Item -type directory -path $paths

  10. This concludes this procedure.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net