Understanding the Variable Provider


The variable provider provides access to the variables that are defined within Windows PowerShell. These variables include both user-defined variables, such as $mred, and system-defined variables, such as $host. You can obtain a listing of the cmdlets designed to work specifically with variables by using the Get-Help cmdlet and specifying the asterisk (*) variable. The commands used in the procedure are in the image from book UnderstandingTheVariableProvider.txt and image from book WorkingWithVariables.txt files. To return only cmdlets, we use the Where-Object cmdlet and filter on the category that is equal to cmdlet. This command is shown here:

 Get-Help *variable | Where-Object {$_.category -eq "cmdlet"}

The resulting list contains five cmdlets but is a little jumbled and difficult to read. So let’s modify the preceding command and specify the properties to return. To do this, use the up arrow and pipeline the returned object into the Format-List cmdlet. Add the three properties we are interested in: name, category, and synopsis. The revised command is shown here:

 Get-Help *variable | Where-Object {$_.category -eq "cmdlet"} | Format-List name, category, synopsis

The resulting output is much easier to read and understand. It is shown here:

 Name     : Get-Variable Category : Cmdlet Synopsis : Gets the variables in the current console.  Name     : New-Variable Category : Cmdlet Synopsis : Creates a new variable.  Name     : Set-Variable Category : Cmdlet Synopsis : Sets the value of a variable. Creates the variable if one with the requested  name does not exist.  Name     : Remove-Variable Category : Cmdlet Synopsis : Deletes a variable and its value.  Name     : Clear-Variable Category : Cmdlet Synopsis : Deletes the value of a variable.

Working with variables

  1. Open Windows PowerShell.

  2. Use the Set-Location cmdlet to set the working location to the variable PSDrive. Use the sl alias to reduce typing needs. This command is shown here:

     SL variable:\

  3. Produce a complete listing of all the variables currently defined in Windows PowerShell. To do this, use the Get-ChildItem cmdlet. You can use the alias gci to produce this list. The command is shown here:

     Get-ChildItem

  4. The resulting list is jumbled. Use the up arrow to retrieve the Get-ChildItem command, and pipeline the resulting object into the Sort-Object cmdlet. Sort on the name property. This command is shown here:

     Get-ChildItem | Sort {$_.Name}

  5. The output from the previous command is shown here:

     Name                           Value ----                           ----- $                              } ?                              True ^                              Get-ChildItem _ args                           {} ConfirmPreference              High ConsoleFileName DebugPreference                SilentlyContinue Error                          {System.Management.Automation.ParseException:... ErrorActionPreference          Continue ErrorView                      NormalView ExecutionContext               System.Management.Automation.EngineIntrinsics false                          False FormatEnumerationLimit         4 HOME                           C:\Documents and Settings\edwils.NORTHAMERICA Host                           System.Management.Automation.Internal.Host.In... input                          System.Array+SZArrayEnumerator LASTEXITCODE                   0 lastWord                       get-c line                           get-c MaximumAliasCount              4096 MaximumDriveCount              4096 MaximumErrorCount              256 MaximumFunctionCount           4096 MaximumHistoryCount            64 MaximumVariableCount           4096 mred                           mred MyInvocation                   System.Management.Automation.InvocationInfo NestedPromptLevel              0 null OutputEncoding                 System.Text.ASCIIEncoding PID                            292 PROFILE                        C:\Documents and Settings\edwils.NORTHAMERICA... ProgressPreference             Continue PSHOME                         C:\WINDOWS\system32\WindowsPowerShell\v1.0 PWD                            Variable:\ ReportErrorShowExceptionClass  0 ReportErrorShowInnerException  0 ReportErrorShowSource          1 ReportErrorShowStackTrace      0 ShellId                        Microsoft.PowerShell StackTrace                        at System.Number.StringToNumber(String str... true                           True VerbosePreference              SilentlyContinue WarningPreference              Continue WhatIfPreference               0

  6. Use the Get-Variable cmdlet to retrieve a specific variable. Use the ShellId variable. You can use Tab completion to speed up typing. The command is shown here:

     Get-Variable ShellId

  7. Use the up arrow to retrieve the previous Get-Variable ShellId command. Pipeline the object returned into a Format-List cmdlet and return all properties. This is shown here:

     Get-Variable ShellId | Format-List *

  8. The resulting output includes the description of the variable, value, and other information shown here:

     Name        : ShellId Description : The ShellID identifies the current shell.  This is used by #Requires. Value       : Microsoft.PowerShell Options     : Constant, AllScope Attributes  : {}

  9. Create a new variable called administrator. To do this, use the New-Variable cmdlet. This command is shown here:

     New-Variable administrator

  10. Use the Get-Variable cmdlet to retrieve the new administrator variable. This command is shown here:

     Get-Variable administrator

  11. The resulting output is shown here. Notice that there is no value for the variable.

     Name                           Value ----                           ----- administrator

  12. Assign a value to the new administrator variable. To do this, use the Set-Variable cmdlet. Specify the administrator variable name, and supply your given name as the value for the variable. This command is shown here:

     Set-Variable administrator -value mred

  13. Use the up arrow one time to retrieve the previous Get-Variable administrator command. This command is shown here:

     Get-Variable administrator

  14. The output displays both the variable name and the value associated with the variable. This is shown here:

     Name                           Value ----                           ----- administrator                  mred

  15. Use the Remove-Variable cmdlet to remove the administrator variable you previously created. This command is shown here:

     Remove-Variable administrator

  16. Use the up arrow one time to retrieve the previous Get-Variable administrator command. This command is shown here:

     Get-Variable administrator

  17. The variable has been deleted. The resulting output is shown here:

     Get-Variable : Cannot find a variable with name 'administrator'. At line:1 char:13 + Get-Variable <<<< administrator

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