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

In the beta version of Windows Threshold, I can run:

$env:username 

And look at the username. I can also run:

 [environment]::username 

And look at the username.

However, although I can run

 $env:computername 

To see the host name, try running:

 [environment]::computername 

does not show any results.

enter image description here

Why does [environment]::computername not work? What is the difference between $env and [environment]?

+6
source share
2 answers

try using

 [environment]::machinename 

$env directly related to the environment variable

[environment] is a .net class

+5
source

When you use [environment]::computername , you really use the .NET code in Powershell. Therefore, reading the documentation for the environment class indicates that [environment]::machinename should be used [environment]::machinename .

$env is a shortcut for the PSDrive environment.

I do not know why Microsoft uses two different names here.

BTW: There are many more ways to get computer_name, you can also, for example, use wmi: Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name

+3
source

All Articles