PS: Using GetEnvironmentVariable to Manage Environment Variables

Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know. Find more tips in the Windows PowerShell Tip of the Week archive. Creating and Modifying Environment Variables Most people know how easy it is to use Windows PowerShell to retrieve information about environment variables. Want to see all your environment variables and their values? This command should do the trick: Copy Get-ChildItem Env: In […]

Read more

PS: Working with Environment Provider

PROVIDER NAME Environment DRIVES Env: SHORT DESCRIPTION Provides access to the Windows environment variables. DETAILED DESCRIPTION The Windows PowerShell Environment provider lets you get, add, change, clear, and delete Windows environment variables in Windows PowerShell. The Environment provider is a flat namespace that contains only objects that represent the environment variables. The variables have no child items. Each environment variable is an instance of the System.Collections.DictionaryEntry class. The name of the variable is the dictionary key. The value of the environment variable is the dictionary value. The Environment provider exposes its data store in the Env: drive. To work with […]

Read more

PS: Best Practices Referring Environment Variables in Powershell Programming

A very good practice to retrieve environment variables values is to refer them as scalar values via $env context   PS C:> echo $env:appdata C:UsersgovardhanAppDataRoaming PS C:> write-host $env:appdata C:UsersgovardhanAppDataRoaming PS C:> If you are in practice of using Get-Item to retrieve environment variables, be cautious on possible typo of using $Env which would fail as shown below, so don’t use $Env while retrieving environment variables via Get-Item.   PS C:> Get-Item Env:UserName Name                           Value —-                           —– USERNAME                       Govardhan PS C:> Get-Item $Env:UserName Get-Item : Cannot find path ‘C:Govardhan’ because it does not exist. At line:1 char:9 + Get-Item <<<<  […]

Read more