9

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]?

2 Answers 2

5

try using

[environment]::machinename 

$env is directly bound to enviroment variable

[environment] is a .net class

Sign up to request clarification or add additional context in comments.

4 Comments

So are there two environments? A traditional shell environment, with its own keys and values, and a separate .net environment?
@mikemaccana The .net class also retrieve value from environmet variable but as a class had methods and properties. Example: ` [environment]::GetEnvironmentVariable("computername")` reads the enironment variale but [environment]::machinename read the netbios name directly.
Thanks @CB. Is there ultimately one data store in total or two?
@mikemaccana I think more then one. If you set $env:computername = "Jack" and try read again with $env and with .net class you have different results.
4

When you use [environment]::computername you are in fact using .NET code in Powershell. So reading the documentation for the Environment Class reveals that you should use [environment]::machinename instead.

$env is a shortcut for the environment PSDrive.

I don't know why Microsoft uses two different names here.

BTW: There are a lot more ways to get the computername, you could also e.g. use wmi: Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name

1 Comment

another way is just using hostname.exe ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.