Why are there two standards for accessing environment variables in Windows when they call point to the same environment variables?
I can use $env:<env-name>
in Powershell to access an environment variable, and in cmd
and all other parts of Windows I can use %env-name%
to access them.
Why is are there two different standards for this if we're only using one operating system?
CMD and Powershell are different technologies from different eras. Most of the CMD syntax rules go back to the '80s.
Powershell is an entirely different shell designed to compete with an entirely different class of software ( ie bash). as such, it needs a more sophisticated syntax, and MS took the opportunity to implement one, both for efficiency, and to give PS a particular "flavor", just like every other major shell.
you might as well ask why python and c# use different syntax.
While in CMD you are really just accessing the environment variables, in PowerShell, you are working with a whole Environment drive. There is a lot more logic behind it, which gives you more opportunities and more flexibility. You can read more about environment variables in PowerShell here and here. Just some examples:
The Environment drive Env
is listed besides all other drives. Execute the following cmdlet to see it:
Get-PSDrive
You can access this drive (mostly) like you access a FileSystem drive. For example, you can list all environment variables by executing:
Get-ChildItem Env:
You can also get a particular environment variable by executing:
Get-Item Env:TMP
To just get the value, you can execute:
(Get-Item Env:TMP).Value
or (as you already did):
$Env:TMP
So, PowerShell uses a whole different technology to work with environment variables. That's why it uses a different syntax, which is defined as follows btw.:
$Env:TMP
In this syntax, the dollar sign (
$
) indicates a variable, and the drive name (Env:
) indicates an environment variable followed by the variable name (TMP
).