June 20th, 2015

PowerTip: Use PowerShell to Get Computer Name

Doctor Scripto
Scripter

Summary: Use Windows PowerShell to get your computer name.

Hey, Scripting Guy! Question How can I use Windows PowerShell to easily retrieve my computer name?

Hey, Scripting Guy! Answer Use the environmental variable ComputerName:

$env:computername

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

6 comments

Discussion is closed. Login to edit/delete existing comments.

  • Callan Browne

    Hi,
    this doesn’t seem to work with Powershell core on my MacBook, is there a new way in powershell core?
    thanks,

    • Michal Zindulka

      The ComputerName is not defined in any variable on PowerShell core running on macOS, as far as I know. You can use the following alternative on MacBook:

      Invoke-Expression -Command 'hostname'

      or shorter:

      iex hostname
  • Aneela Yekula

    Can anyone help me out,im not getting Computername through $env:COMPUTERNAME command…its returning as ‘COMPUTERNAME’  not my computername

    • p m

      Type “$env:computername” (without the quotes) at the PowerShell prompt, and it will return the computer name — it should work, whether or not “computername” is capitalized.
      You can also try:$(Get-WmiObject Win32_Computersystem).name

  • Aneela Yekula

    Can anyone help me out,im not getting Computername through $env:COMPUTERNAME command…its returning as ‘COMPUTENAME’  not my computername

    • Bharath Radhekrishna

      $env:computername | Select-Object