![Get the Computer Name cross-platform using Powershell](https://www.justinjbird.me/images/apps/powershell.webp)
Get the Computer Name cross-platform using Powershell
- coding
- 2023-12-10
- 1 minute to read
- powershell
Table of Contents
Introduction
There are a few ways to get the computer name using Powershell. One way is to use the environment variable $env:COMPUTERNAME
however, this doesn’t work on a Mac.
![a screenshot of terminal in vscode with no value returned from the command](https://www.justinjbird.me/posts/2023/get-computer-name-using-powershell/001.webp)
Other options
Here are a few options to get the computer name using Powershell that work consistently on both Windows and Mac.
MachineName property of the Environment class
[Environment]::MachineName
GetHost() method
[System.Net.Dns]::GetHostName()
Using the output
Both of these options will return the computer name as a string allowing you to pass the value into a variable.
$name = [System.Net.Dns]::GetHostName()
Write-Host "Computer name is ${name}"
> Computer name is Justin-MacStudioM2.local
$name = [System.Net.Dns]::GetHostName()
Write-Host "Computer name is ${name}"
> Computer name is Justin-MacStudioM2.local
![the jedi order logo](/images/star-wars/jedi.webp)
#mtfbwy