Quick Hits: Getting the Local Computer Name

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 Smile) 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

image

A more simple approach is making use of a legacy command, hostname.exe which will present you with the local machine’s computername.

image

There are also environmental variables that you can make use of to get this information like COMPUTERNAME.

$Env:Computername

image

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!

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s