I’ve had a few emails and questions about the best way to get the hostname of the local computer that you are currently logged into and felt that a quick (and I mean quick ) post here would be worthwhile.
Using WMI or the CIM cmdlets, you can pull the information from just about any class as there is the __Server property which gives you the name, or you can make of the PSComputername property (which is just an alias to __Server thanks to a type file). Here is one such example:
Get-WMIObject –Class Win32_Bios | Select PSComputername, __Server
A more simple approach is making use of a legacy command, hostname.exe which will present you with the local machine’s computername.
There are also environmental variables that you can make use of to get this information like COMPUTERNAME.
$Env:Computername
That’s it! Like I said, this was short and sweet. Do you know an other ways to display the hostname of your local machine? Comment with them below!