How to Get the Computer Name in PowerShell?

get computer name powershell

Knowing how to get a computer’s name is essential for any Windows system administrator working with PowerShell. You’ll often need to retrieve and use computer names in various administration tasks like remote management, reporting, and scripting. PowerShell provides a comprehensive set of tools and cmdlets to make this process efficient and straightforward. In this comprehensive guide, I will walk you through the various methods for getting a computer name in PowerShell. Whether you’re a beginner or an experienced user, this step-by-step guide will help you master PowerShell.

Key Takeaways:

  • PowerShell provides various methods to retrieve the computer name.
  • The hostname command is a quick and easy way to retrieve the computer name in PowerShell.
  • The System.Net.DNS class provides methods like GetHostName() and GetHostByAddress() to retrieve hostnames and IP addresses.
  • PowerShell allows access to environment variables, such as $Env:COMPUTERNAME, to retrieve the computer name.
  • Using WMI and CIM, you can query computer information and retrieve the computer name in PowerShell.

Introduction to Getting the Computer Name with PowerShell

Getting the computer name is a vital task for system administrators and IT professionals, for various purposes. Whether you need to automate processes, track system information, or troubleshoot issues, knowing the computer name is essential. In this section, I will introduce you to different methods and commands that you can use to retrieve the computer name effortlessly using PowerShell.

Before we dive into the various methods for getting a computer name in PowerShell, it’s important to understand what a computer name is and how it is used in PowerShell. A computer name is a unique identifier that is assigned to each computer on a network. It is used to identify the computer on the network and to allow users to connect to it.

Why is retrieving the computer name important?

Understanding the computer name is crucial for system administration and troubleshooting tasks. When working in a networked environment, being able to identify specific machines is essential for efficient management. Knowing the computer names in a network is crucial for various administrative tasks. Whether you need to perform software installations, troubleshoot connectivity issues, or manage user permissions, having access to computer names is essential.

In PowerShell, the computer name is used as a parameter for many cmdlets. For example, the Get-Service cmdlet is used to retrieve information about services on a computer. To use this cmdlet, you need to specify the computer name as a parameter. Here are a few simple ways to get the local computer’s name in PowerShell:

How to Get the Computer Name in PowerShell?

How to Get the Computer Name in PowerShell

To get the computer name in PowerShell, you can use several methods. Here are some of the most common ones:

  1. Using the HostName command
  2. Using Get-ComputerInfo cmdlet
  3. Using the WMIObject
  4. Using Environment Variables
  5. Using the .NET Framework

Method 1: Use the hostname command to find the computer name

In PowerShell, you can easily get the name of the computer on which the script or command is being executed by using the ”hostname” command:

hostname

This command returns the name of the computer you’re running PowerShell on.

Method 2: Using the Get-ComputerInfo cmdlet

The simplest way to get the computer name in PowerShell is to use the Get-ComputerInfo cmdlet. This cmdlet returns the name of the local computer.

Get-ComputerInfo -Property CSName

#Also works:(Get-ComputerInfo).CSName

If you run this command in a PowerShell window, it will return the name of the computer that you are currently using. This is the quickest and easiest way to get the computer name in PowerShell. As we saw above, the Get-ComputerInfo cmdlet is used to retrieve the name of the local computer. However, there are other ways to obtain this information in PowerShell.

Method 3: Using the WMIObject to get the computer name in PowerShell

PowerShell provides powerful capabilities for retrieving computer information by using Windows Management Instrumentation (WMI) and Common Information Model (CIM). These features enable you to query different aspects of a computer system, including the computer name.

To get the computer name using the Get-WmiObject cmdlet, you need to specify the class name and the property that you want to retrieve. In this case, we want to retrieve the name of the computer, which is stored in the Win32_ComputerSystem class.

Get-WmiObject Win32_ComputerSystem | Select-Object Name

#Also works: (Get-CimInstance -ClassName Win32_ComputerSystem).Name

When you run this command, it may prompt you to enter the credentials for remote computers. Once you enter the credentials, it will return the name of the remote computer.

Method 4: Getting the current computer name from the Environment Variable

When it comes to retrieving the computer name in PowerShell, one of the most convenient and commonly used methods is through environment variables. Specifically, PowerShell provides access to the COMPUTERNAME environment variable, which stores the computer name we are looking for.

The $env:COMPUTERNAME variable contains the name of the computer that the user is logged on to.

$env:COMPUTERNAME

This command will return the name of the local computer, that the user is logged on to. To retrieve the fully qualified domain name (FQDN) of the local computer, including the domain, use:

$env:userdnsdomain + "\" + $env:computername

Method 5: Find the Computer Name Using the .NET Framework

You can use the .NET [System.Environment] class to get the machine name:

[System.Environment]::MachineName

#Also works: [Environment]::MachineName

You can also use:

[System.Net.Dns]::GetHostName()

PowerShell will return the name of your computer.

Get the Host Name from an IP Address

To get a computer’s name from its IP address, use one of these options:

# Set the IP Address
$IPAddress = "127.0.0.1"

#Get Host Name by IP
[System.Net.Dns]::GetHostbyAddress($IPAddress) | Select-Object -ExpandProperty HostName

#Resolve IP Address
[System.Net.Dns]::Resolve($IPAddress) | Select-Object -ExpandProperty HostName

#Resolve DNS
Resolve-DnsName -Name $IPAddress | Select-Object -ExpandProperty NameHost

All perform a reverse DNS lookup, querying the DNS server to resolve the IP back to a hostname. You can also use the WMI object as:

$RemoteComputer = "192.160.10.1"
$ComputerName = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $RemoteComputer).Name
Write-Host "Computer name for $RemoteComputer $computerName"

Getting a Remote Computer’s Name

get computer name in powershell

While the methods that we have covered so far are sufficient for most tasks, there are some more advanced techniques that you can use to get computer names in PowerShell. For remote computer name retrieval, we need to use PowerShell remoting. Here are some examples of domain-joined and workgroup systems:

For example, you can use the Invoke-Command cmdlet to run commands on a remote computer. This cmdlet allows you to run commands on multiple computers at once, which can be useful for managing large-scale environments.

Invoke-Command -ComputerName "192.168.1.1", "192.168.1.2" -ScriptBlock { $env:computername }

This command will return the names of the computers, “Computer01” and “Computer02”.

Conclusion and next steps

Getting the name of your computer in PowerShell is a simple task that can be accomplished in a few different ways. In this step-by-step guide, we explored various methods to retrieve computer names using PowerShell. Whether you prefer to use the hostname command, the .NET Framework, or the WMI object, PowerShell has you covered.

With these methods, you’ll quickly and easily access the information you need to keep your system running smoothly. Refer to this article anytime you need to obtain, use, or manage computer names in your PowerShell scripts and administration.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “How to Get the Computer Name in PowerShell?

  • The Get-ComputerName cmdlet does not exist

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *