Let us first list out the variables we have. This can be done in two ways:
- Using a cmdlet
- Using a provider
Let us first look at using the cmdlet to list out the variables built into PowerShell.
- Open a terminal window. If you have one open, restart PowerShell.
- Find the cmdlet that works with variables.
PS> Get-Command -Noun Variable
Remember that the noun in a cmdlet is always singular. Therefore, it would be Variable, and not Variables.
- There are five cmdlets that deal with variables. We want to fetch a list of all variables already existing in a new session of PowerShell. Let us pick Get-Variable, and fetch help information for it.
PS> Get-Help Get-Variable
- This is the cmdlet that we need to list out all the variables predefined in the current scope.
PS> Get-Variable
That should list out all the variables predefined in the current scope.
Any variables you define would be listed here. Hence, it is important that you start a fresh session of PowerShell to see what variables have been predefined.
Let us now use a PowerShell provider to list out the variables defined in the current scope.
- List out the PowerShell providers. We looked at providers in the previous chapter.
PS> Get-PsProvider
- Change location to the Variable: drive of the Variable provider. This is done using Set-Location.
PS> Set-Location Variable:
- Now, let us list out all the available child items of the Variable: drive.
PS> Get-ChildItem .
The output of this was identical to that of Get-Variable, called without an argument.