---
title: "Quickly Get Computer Names with PowerShell"
description: "Discover various methods to use PowerShell to get computer names in this easy-to-follow tutorial."
canonical: "https://adamtheautomator.com/powershell-to-get-computer-name/"
---

# Quickly Get Computer Names with PowerShell

> Discover various methods to use PowerShell to get computer names in this easy-to-follow tutorial.

Source: https://adamtheautomator.com/powershell-to-get-computer-name/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Quickly Get Computer Names with PowerShell](https://adamtheautomator.com/wp-content/uploads/2020/11/How-to-Get-a-Computer-Name-with-PowerShell.png)

# Quickly Get Computer Names with PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)4 November 20204 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Using the Hostname command in PowerShell to Get Computer Name](#h-using-the-hostname-command)
*   [Using the System.Net.DNS .NET Class](#h-using-the-system-net-dns-net-class)
*   [The GetHostName() Method](#h-the-gethostname-method)
*   [The GetHostByName() Method](#h-the-gethostbyname-method)
*   [Using Environment Variables](#h-using-environment-variables)
*   [The ComputerName Environment Variable](#h-the-computername-environment-variable)
*   [The MachineName Property](#h-the-machinename-property)
*   [Using WMI](#h-using-wmi)
*   [Finding Remote Computer Names](#h-finding-remote-computer-names)
*   [Using PowerShell Remoting](#h-using-powershell-remoting)
*   [Using WMI](#h-using-wmi-1)
*   [Next Steps](#h-next-steps)

Do you need to find a local or remote Windows computer’s name in a PowerShell script? Look no further. In this tutorial, you’re going to learn how to use PowerShell to get computer name in a few different and sometimes unexpected ways.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

## Prerequisites

This article will be a hands-on tutorial. If you’d like to apply the concepts you learn in this tutorial, please be sure you have PowerShell. That’s it! The article will be using PowerShell 7 (the latest as of this writing) but Windows PowerShell will probably work just as well.

## Using the `Hostname` command in PowerShell to Get Computer Name

Back before the days of PowerShell, the only Windows command interpreter we had was good ol’ _cmd .exe_. Back then, we didn’t need no stinkin’ PowerShell to get a computer name; we had the `hostname` command!

The `hostname` command couldn’t be any simpler. Open up a PowerShell (or even cmd .exe prompt) and type `hostname`. Done. This command returns a single string (the computer name of the local computer).

![Running the hostname command](https://adamtheautomator.com/wp-content/uploads/2020/11/Untitled-61.png)

Running the hostname command

## Using the `System.Net.DNS` .NET Class

If you’d like to go the more PowerShell-centric approach, you can also reference a static method called `GetHostByName()` located in the `System.Net.DNS` .NET class or perhaps the `GetHostName()` method.

### The `GetHostName()` Method

Using the `GetHostName()` method is probably the easiest way to use PowerShell to get a computer name. Simply call this static method with no arguments as shown below. This command will return a single string just like the `hostname` command does.

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

### The `GetHostByName()` Method

An alternative but similar `System.Net.DNS` class method you can use to get a computer name is called `GetHostByName()`. This method is actually a DNS resolver and can be used to look up other host names as well.

If, for example, you’d like to find the host name of the local computer, run `[System.NET.DNS]::GetHostByName($null)` or `[System.NET.DNS]::GetHostByName('')`.

```powershell
[System.Net.DNS]::GetHostByName('')
[System.Net.DNS]::GetHostByName($Null)
```

You’ll see that this method isn’t strictly meant for finding computer names; you can also lookup IP addresses as well via the `AddressList` property as shown below.

![Using the GetHostByName() method with PowerShell to get a computer name](https://adamtheautomator.com/wp-content/uploads/2020/11/Untitled-62-1024x229.png)

Using the GetHostByName() method

If, however, you want to only find the local computer name; reference the `HostName` property, and PowerShell will only return the hostname.

```powershell
[System.Net.DNS]::GetHostByName('').HostName
[System.Net.DNS]::GetHostByName($Null).HostName
```

## Using Environment Variables

[Environment variables](https://adamtheautomator.com/powershell-environment-variables/) are always a great place to find information about Windows computers; using PowerShell to get a computer name is no different.

### The ComputerName Environment Variable

Every Windows computer stores an environment variable called `ComputerName`. Like all other environment variables, you can access user environment variables via the `$env` PowerShell construct.

To reference the `COMPUTERNAME` environment variable, open up PowerShell and preface the environment variable name with `$env:`. PowerShell will then return the local computer name as a single string.

```powershell
$env:COMPUTERNAME
```

### The MachineName Property

Alternatively, if, for some reason, the user-based `COMPUTERNAME` environment variable doesn’t work in your situation, you can also use the `MachineName` property that’s part of the .NET `Environment` class.

Reference the `MachineName` property in the `Environment` .NET class as shown below.

```powershell
[Environment]::MachineName
```

## Using WMI

Related:[Leverage PowerShell WMI Cmdlets to Explore Any Windows Computer](https://adamtheautomator.com/powershell-wmi/)

Finally, you’ve always got the option of going into WMI or CIM. You probably should make this your last resort as it will require the most overhead albeit tiny. Using PowerShell to get a computer name with WMI would be best to query remote computer names.

If you’d like to use WMI to query a local computer name, use [`Get-CimInstance`](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2) to query the _Win32\_ComputerSystem_ class as shown below. Since `Get-CimInstance` doesn’t return the computer name but an object representing a CIM instance, reference the `Name` property to only return the computer name.

```powershell
Get-CimInstance -ClassName Win32_ComputerSystem
(Get-CimInstance -ClassName Win32_ComputerSystem).Name
```

![Win32\_ComputerSystem Query](https://adamtheautomator.com/wp-content/uploads/2020/11/Untitled-63-1024x219.png)

Win32\_ComputerSystem Query

## Finding Remote Computer Names

Perhaps you manage many computers and need to find the hostname across all of them. Typically, the hostname will be represented in Active Directory (AD) if you’re in that kind of environment or collected by some other asset management tool. But if it’s not, you can always fall back to PowerShell.

To use PowerShell to get remote computer names, you have two options. You can either wrap the methods you learned above in a [PowerShell Remoting](https://adamtheautomator.com/psremoting/ "PowerShell Remoting") scriptblock or use WMI.

### Using PowerShell Remoting

Related:[PowerShell Remoting: The Ultimate Guide](https://adamtheautomator.com/psremoting/)

Rather than cover each method cover earlier again in this section, just know this. You can wrap _any_ local command in a PowerShell Remoting scriptblock.

If, for example, you have PowerShell Remoting enabled on a remote computer, you can put any of the above methods inside of a scriptblock and execute that scriptblock with the [`Invoke-Command` command](https://adamtheautomator.com/invoke-command/).

Assuming you only have an IP address and need to find the hostname of a computer with the IP address of 192.168.1.2 and you’re not in an AD environment, call `Invoke-Command` like below.

```powershell
## Create the pscredential object to pass to Invoke-Command
$credential = Get-Credential

## Run the command on the remote computer
Invoke-Command -ComputerName 192.168.1.2 -ScriptBlock { [System.Net.Dns]::GetHostName() } -Credential $credential
```

If PowerShell Remoting was able to connect to the remote computer, PowerShell will return the same output as you’d see if you were running this command locally.

### Using WMI

Alternatively, you can also use WMI to use PowerShell to get a computer name without having to wrap a command inside of a scriptblock. The process to find a remote computer name is nearly the same as locally; simply use the `ComputerName` parameter.

```powershell
Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName 192.168.1.2
(Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName 192.168.1.2).Name
```

The `Get-CimInstance` requires authentication. If you’re in an AD environment, you will not need to supply any credentials as seen above. However, if you’re not in an AD environment, you’ll need to establish a new CIM session with the `New-CimSession` cmdlet, use `Get-CimInstance` to use that session, and then remove the CIM session yourself. Below is a code snippet on how to do that.

```powershell
## Create the PSCredential object
$credential = Get-Credential

## Connect to the remote computer passing the creds and creating a remote session
$cimSession = New-CimSession -ComputerName 192.168.1.5 -Credential $credential

## Use the session to query WMI and reference the Name property
(Get-CimInstance -Session $cimSession -ClassName Win32_ComputerSystem).Name
```

## Next Steps

If you’ve read through this entire tutorial, you should now have nearly all of the most popular ways to use PowerShell to get a computer name. Although there are probably many more we’ve missed, these are all of the ways you’ll find this task accomplished in many scripts.

Now that you know how to accomplish this task, try to build a script that will get computer names in bulk with a loop perhaps via a CSV or text file!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-to-get-computer-name%2F&text=Quickly%20Get%20Computer%20Names%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-to-get-computer-name%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-to-get-computer-name%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
