Local computer host name

Overview

Among other tasks, when writing PowerShell scripts it’s necessary to obtain the host name of the local computer. There are two ways this can be achieved.

Method 1

Access the Windows environment variable computername.

$computername=Get-Content env:computername;

Method 2

From the .NET Framework method GetHostName.

$computername=[System.Net.Dns]::GetHostName();

Reference

about_Environment_Variables This Microsoft TechNet article describes how to access Windows environment variables in Windows PowerShell.

.NET Framework Class System.Net.Dns This class provides simple domain name resolution functionality, method GetHostName will return the host name of the local computer.

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s