Understanding the Environment Provider


The environment provider in Windows PowerShell is used to provide access to the system environment variables. If you open a CMD (command) shell and type set, you will obtain a listing of all the environment variables defined on the system. If you use the echo command in the CMD shell to print out the value of %windir%, you will obtain the results seen in Figure 3-5.

image from book
Figure 3-5: Use set in a CMD prompt to see environment variables

Environment variables are used by various applications and other utilities as a shortcut to provide easy access to specific files, folders, and configuration data. By using the environment provider in Windows PowerShell, you can obtain a listing of the environment variables. You can also add, change, clear, and delete these variables.

Obtaining a listing of environment variables

  1. Open Windows PowerShell.

  2. Obtain a listing of the PSDrives by using the Get-PSDrive cmdlet. This is shown here:

     Get-PSDrive

  3. Note that the Environment PSDrive is called env. Use the env name with the Set-Location cmdlet and change to the environment PSDrive. This is shown here:

     Set-Location env:\

  4. Use the Get-Item cmdlet to obtain a listing of all the environment variables on the system. This is shown here:

     Get-Item *

  5. Use the Sort-Object cmdlet to produce an alphabetical listing of all the environment variables by name. Use the up arrow to retrieve the previous command, and pipeline the returned object into the Sort-Object cmdlet. Use the property argument, and supply name as the value. This command is shown here:

     get-item * | Sort-Object  -property name

  6. Use the Get-Item cmdlet to retrieve the value associated with the environment variable windir. This is shown here:

     get-item windir

  7. Use the up arrow and retrieve the previous command. Pipeline the object returned to the Format-List cmdlet and use the wild card character to print out all the properties of the object. The modified command is shown here:

     get-item windir | Format-List *

  8. The properties and their associated values are shown here:

     PSPath        : Microsoft.PowerShell.Core\Environment::windir PSDrive       : Env PSProvider    : Microsoft.PowerShell.Core\Environment PSIsContainer : False Name          : windir Key           : windir Value         : C:\WINDOWS

  9. This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.

Creating a new environment variable

  1. You should still be in the Environment PSDrive from the previous procedure. If not, use the Set-Location env:\ command).

  2. Use the Get-Item cmdlet to produce a listing of all the environment variables. Pipeline the returned object to the Sort-Object cmdlet using the property of name. To reduce typing, use the gi alias and the sort alias. This is shown here:

     GI * | Sort -Property Name

  3. Use the New-Item cmdlet to create a new environment variable. The path argument will be dot (.) because you are already on the env:\ PSDrive. The -name argument will be admin, and the value argument will be your given name. The completed command is shown here:

     New-Item -Path . -Name admin -Value mred

  4. Use the Get-Item cmdlet to ensure the admin environment variable was properly created. This command is shown here:

     Get-Item admin

  5. The results of the previous command are shown here:

     Name                           Value ----                           ----- admin                          mred

  6. Use the up arrow to retrieve the previous command. Pipeline the results to the FormatList cmdlet, and choose All Properties. This command is shown here:

     Get-Item admin | Format-List *

  7. The results of the previous command include the PSPath, PSDrive, and additional information about the newly created environment variable. These results are shown here:

     PSPath        : Microsoft.PowerShell.Core\Environment::admin PSDrive       : Env PSProvider    : Microsoft.PowerShell.Core\Environment PSIsContainer : False Name          : admin Key           : admin Value         : mred

  8. This concludes this procedure. Leave PowerShell open for the next procedure.

Renaming an environment variable

  1. Use the Get-ChildItem cmdlet to obtain a listing of all the environment variables. Pipeline the returned object to the Sort-Object cmdlet and sort the list on the name property. Use the gci and sort aliases to reduce typing. The code to do this is shown here:

     GCI | Sort -Property name

  2. The admin environment variable should be near the top of the list of system variables. If it is not, then create it by using the New-Item cmdlet. The path argument has a value of dot (.); the name argument has the value of admin; and the value argument should be the user’s given name. If this environment variable was created in the previous exercise, then PowerShell will report that it already exists. This is shown here:

     New-Item -Path . -Name admin -Value mred

  3. Use the Rename-Item cmdlet to rename the admin environment variable to super. The path argument combines both the PSDrive name and the environment variable name. The NewName argument is the desired new name without the PSDrive specification. This command is shown here:

     Rename-Item -Path env:admin -NewName super

  4. To verify that the old environment variable admin has been renamed super, press the up arrow two or three times to retrieve the gci | sort -property name command. This is command is shown here:

     GCI | Sort -Property name

  5. This concludes this procedure. Do not close the Windows PowerShell. Leave it open for the next procedure.

Removing an environment variable

  1. Use the Get-ChildItem cmdlet to obtain a listing of all the environment variables. Pipeline the returned object to the Sort-Object cmdlet and sort the list on the name property. Use the gci and sort aliases to reduce typing. The code to do this is shown here:

     GCI | Sort -Property name

  2. The super environment variable should be in the list of system variables. If it is not, then create it by using the New-Item cmdlet. The path argument has a value of dot (.); the name argument has the value of super; and the value argument should be the user’s given name. If this environment variable was created in the previous exercise, then PowerShell will report that it already exists. This is shown here:

     New-Item -Path . -Name super -Value mred

  3. Use the Remove-Item cmdlet to remove the super environment variable. The name of the item to be removed is typed following the name of the cmdlet. If you are still in the env:\ PSDrive, you will not need to supply a -path argument. The command is shown here:

     Remove-Item super

  4. Use the Get-ChildItem cmdlet to verify that the environment variable super has been removed. To do this, press the up arrow 2 or 3 times to retrieve the gci | sort -property name command. This command is shown here:

     GCI | Sort -Property name

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