| description | Variable |
|---|---|
| Locale | en-US |
| ms.date | 10/18/2018 |
| online version | https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_variable_provider?view=powershell-7.6&WT.mc_id=ps-gethelp |
| schema | 2.0.0 |
| title | about_Variable_Provider |
Variable
Variable:
ShouldProcess
Provides access to the PowerShell variables and to their values.
The PowerShell Variable provider lets you get, add, change, clear, and delete PowerShell variables in the current console.
The PowerShell Variable provider supports the variables that PowerShell creates, including the automatic variables, the preference variables, and the variables that you create.
The Variable drive is a flat namespace that contains only the variable objects. The variables have no child items.
The Variable provider supports the following cmdlets, which are covered in this article.
- Get-Location
- Set-Location
- Get-Item
- New-Item
- Remove-Item
- Clear-Item
PowerShell also includes a set of cmdlets designed especially to view and to
change variables. When you use Variable cmdlets, you do not need to specify
the Variable: drive in the name. This article does not cover working with
Variable cmdlets.
- Get-Variable
- New-Variable
- Set-Variable
- Remove-Variable
- Clear-Variable
Note
You can also use the PowerShell expression parser to create, view, and change
the values of variables without using the cmdlets. When working with
variables directly, use a dollar sign ($) to identify the name as a
variable and the assignment operator (=)to establish and change its value.
For example, $p = Get-Process creates the p variable and stores the
results of a Get-Process command in it.
Variables can be one of several different types. Most variables will be
instances of the PSVariable class. Other variables and their types are
listed below.
- The
?variable is an instance of theQuestionMarkVariableclass. - The
nullvariable is an instance of theNullVariableclass. - The maximum count variables are instances of the
SessionStateCapacityVariableclass. LocalVariableinstances contain information about current execution, such as:MyInvocationPSCommandPathPSScriptRootPSBoundParametersargsinput
The Variable provider exposes its data store in the Variable: drive. To
work with variables, you can change your location to the Variable: drive
(Set-Location Variable:), or you can work from any other PowerShell drive. To
reference a variable from another location, use the drive name (Variable:) in
the path.
Set-Location Variable:To return to a file system drive, type the drive name. For example, type:
Set-Location C:You can also work with the Variable provider from any other PowerShell
drive. To reference a variable from another location, use the drive name
Variable: in the path.
Note
PowerShell uses aliases to allow you a familiar way to work with provider
paths. Commands such as dir and ls are now aliases for
Get-ChildItem, cd is an alias for Set-Location. and pwd is
an alias for Get-Location.
This command gets the list of all the variables and their values in the current session. You can use this command from any PowerShell drive.
Get-ChildItem -Path Variable:This command retrieves a variables value using its provider path prefixed by
the dollar sign ($). This has the same effect as prefixing the variables name
with the dollar sign ($).
$Variable:HOMEThis command gets the variables with names that begin with "Max". You can use this command from any PowerShell drive.
Get-ChildItem -Path Variable:Max*This command uses the -LiteralPath parameter of Get-ChildItem to get
the value of the ? variable from within the Variable: drive. The ? is
a wildcard in paths, but Get-ChildItem does not attempt to resolve any
wildcards in the values of the -LiteralPath parameter.
Get-ChildItem -LiteralPath ?This command gets the variables that have the values of ReadOnly or
Constant for their Options property.
Get-ChildItem -Path Variable: |
Where-Object {
$_.Options -match 'Constant' -or
$_.Options -match 'ReadOnly'
} |
Format-List -Property Name, Value, OptionsThis command creates the services variable and stores the results of a
Get-Service command in it. Because the current location is in the Variable:
drive, the value of the -Path parameter is a dot (.), which represents the
current location.
The parentheses around the Get-Service command ensure that the command is
executed before the variable is created. Without the parentheses, the value of
the new variable is a "Get-Service" string.
New-Item -Path . -Name services -Value (Get-Service)This command creates a services variable and stores the result of a
Get-Service command in it.
New-Item -Path Variable:services -Value Get-ServiceTo create a variable without a value, omit the assignment operator.
This command uses the Rename-Item cmdlet to change the name of the a
variable to processes.
Rename-Item -Path Variable:a -NewName processesThis command uses the Set-Item cmdlet to change the value of the
ErrorActionPreference variable to "Stop".
Set-Item -Path Variable:ErrorActionPreference -Value StopThis command uses the Copy-Item cmdlet to copy the processes variable to
old_processes. This creates a new variable named old_processes that has the
same value as the processes variable.
Copy-Item -Path Variable:processes -Destination Variable:old_processesThis command deletes the serv variable from the current session. You can use
this command in any PowerShell drive.
Remove-Variable -Path Variable:servThis command deletes all variables from the current session except for the
variables whose Options property has a value of Constant. Without the
-Force parameter, the command does not delete variables whose Options
property has a value of ReadOnly.
Remove-Item Variable:* -ForceThis command uses the Clear-Item cmdlet to change the value of the
processes variable to NULL.
Clear-Item -Path Variable:processesProvider cmdlets accept pipeline input. You can use the pipeline to simplify task by sending provider data from one cmdlet to another provider cmdlet. To read more about how to use the pipeline with provider cmdlets, see the cmdlet references provided throughout this article.
Beginning in Windows PowerShell 3.0, you can get customized help topics for provider cmdlets that explain how those cmdlets behave in a file system drive.
To get the help topics that are customized for the file system drive, run a
Get-Help command in a file system drive or use the -Path parameter of
Get-Help to specify a file system drive.
Get-Help Get-ChildItemGet-Help Get-ChildItem -Path Variable: