MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
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.
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
-
Open Windows PowerShell.
-
Obtain a listing of the PSDrives by using the Get-PSDrive cmdlet. This is shown here:
Get-PSDrive
-
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:\
-
Use the Get-Item cmdlet to obtain a listing of all the environment variables on the system. This is shown here:
Get-Item *
-
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
-
Use the Get-Item cmdlet to retrieve the value associated with the environment variable windir. This is shown here:
get-item windir
-
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 *
-
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
-
This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.
Creating a new environment variable
-
You should still be in the Environment PSDrive from the previous procedure. If not, use the Set-Location env:\ command).
-
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
-
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
-
Use the Get-Item cmdlet to ensure the admin environment variable was properly created. This command is shown here:
Get-Item admin
-
The results of the previous command are shown here:
Name Value ---- ----- admin mred
-
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 *
-
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
-
This concludes this procedure. Leave PowerShell open for the next procedure.
Renaming an environment variable
-
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
-
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
-
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
-
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
-
This concludes this procedure. Do not close the Windows PowerShell. Leave it open for the next procedure.
Removing an environment variable
-
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
-
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
-
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
-
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
-
This concludes this procedure.
Категории