Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are [environment] and $env different in powershell?

On Windows Threshold beta, I can run:

$env:username

And see the username. I can also run:

[environment]::username

And see the username.

However while I can run

$env:computername

To see the hostname, trying to run:

[environment]::computername

does not show any results.

enter image description here

Why doesn't [environment]::computername work? What's the difference between $env and [environment]?

like image 542
mikemaccana Avatar asked Aug 25 '15 13:08

mikemaccana


People also ask

What does $ENV mean in PowerShell?

Using the variable syntax In this syntax, the dollar sign ( $ ) indicates a variable, and the drive name ( Env: ) indicates an environment variable followed by the variable name ( windir ). You can create and update the value of environment variables with the following syntax: PowerShell Copy.

What is the difference between Path and environment variable?

1) PATH and Path are the same since Windows environment variables are case insensitive (File paths in Windows environment not case sensitive?). 2) Windows use Path to locate executables that are not located in the "current folder".

What is the difference between variables and environment variables?

The difference between the two is that variables values may change during execution, while constant values cannot be reassigned. An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice.

Where are PowerShell environment variables?

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.


1 Answers

try using

[environment]::machinename

$env is directly bound to enviroment variable

[environment] is a .net class

like image 124
CB. Avatar answered Oct 06 '22 23:10

CB.