How to do it...

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.

  1. Open a terminal window. If you have one open, restart PowerShell.
  2. 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.

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

  1. List out the PowerShell providers. We looked at providers in the previous chapter.
PS> Get-PsProvider
  1. Change location to the Variable: drive of the Variable provider. This is done using Set-Location.
PS> Set-Location Variable:
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.66.13