PowerShell: Get Computer Hostname

Need to check a machine's hostname - for identifying which computer you're on, logging, or scripts that run across multiple machines? This one-liner pulls it straight from the system.

Prerequisites:

  • Privileges: None
  • Module: Built-in, no import needed

Quick Command:

Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object Name

Example Output:

Name
----
PC-IT-01
📦
Want all of them at once?
Get every free one-liner from this blog in a single downloadable bundle organized by category, each with full comment-based help. No more copy-pasting one at a time.

How It Works:

  • Get-CimInstance -ClassName Win32_ComputerSystem queries the computer system class, which holds machine-level info like name, manufacturer, and system type.
  • Select-Object Name trims the output down to just the Name property, since Win32_ComputerSystem also returns a lot more data (like Manufacturer, Model, or TotalPhysicalMemory) that isn't needed for a quick hostname check.