Get Computer Name Using Powershell

Get Computer Name Using Powershell

  • coding
  • 2023-12-10
  • 1 minute to read

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.

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

#mtfbwy



Recent Posts

T-SQL Tuesday #174 My favourite job interview question

T-SQL Tuesday #174 My favourite job interview question

  • 2024-05-14
  • 4 minutes to read

This is my 12th contribution to TSQL Tuesday, the data community blog party. Have a read, and why not join in next time!

Read More
Connecting Azure Data Studio to Postgres

Connecting Azure Data Studio to Postgres

  • 2024-05-09
  • 2 minutes to read

How to connect Azure Data Studio to a Postgres database.

Read More