Skip to main content
TopMiniSite

Back to all posts

How to Get the Hostname From Powershell?

Published on
3 min read
How to Get the Hostname From Powershell? image

Best Powershell Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$50.21
Learn PowerShell Scripting in a Month of Lunches
4 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
5 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
6 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
7 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
+
ONE MORE?

To get the hostname from PowerShell, you can use the cmdlet "hostname" or the .NET framework class "System.Net.Dns" along with the method "GetHostName()" to retrieve the name of the local computer. Simply open a PowerShell window and type either of these commands to display the hostname of the machine.

How to display the hostname along with other system information in PowerShell?

You can display the hostname along with other system information in PowerShell by using the following command:

Get-WmiObject Win32_ComputerSystem | Select Name, Manufacturer, Model, TotalPhysicalMemory, NumberOfProcessors

This command will retrieve information about the computer system, including the hostname (Name), manufacturer, model, total physical memory, and number of processors.

How can I find the hostname of my computer using PowerShell?

You can find the hostname of your computer using the following PowerShell command:

hostname

Simply open PowerShell and type the command hostname and press Enter. This will display the hostname of your computer.

What is the syntax for retrieving the hostname in PowerShell?

The syntax for retrieving the hostname in PowerShell is:

$env:COMPUTERNAME

How to get the full computer name from PowerShell?

To get the full computer name from PowerShell, you can use the following command:

$env:COMPUTERNAME

This command will retrieve the full computer name of the machine you are currently using.

How to incorporate the hostname retrieval into a complete system information gathering script using PowerShell?

To incorporate the hostname retrieval into a complete system information gathering script using PowerShell, you can follow the steps outlined below:

  1. Open Windows PowerShell as an administrator.
  2. Create a new PowerShell script file by running the following command:

New-Item -Path "C:\Scripts\SystemInformation.ps1" -ItemType "File"

  1. Edit the script file using a text editor or PowerShell ISE:

# Get and display the hostname $hostname = (Get-CimInstance Win32_ComputerSystem).Name Write-Output "Hostname: $hostname"

Get and display other system information

$os = Get-CimInstance Win32_OperatingSystem $osName = $os.Caption $osVersion = $os.Version $processor = (Get-CimInstance Win32_Processor).Name $memory = [math]::Round(($os.TotalVisibleMemorySize / 1mb), 2) $uptime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime $uptime = (Get-Date) - $uptime

Write-Output "Operating System: $osName" Write-Output "OS Version: $osVersion" Write-Output "Processor: $processor" Write-Output "Memory: ${memory}GB" Write-Output "Uptime: $($uptime.Days) days, $($uptime.Hours) hours, $($uptime.Minutes) minutes"

  1. Save the changes to the script file.
  2. To run the script, navigate to the directory where the script file is located and execute the following command:

.\SystemInformation.ps1

This script will retrieve and display the hostname of the system, as well as other system information such as the operating system name and version, processor information, memory amount, and system uptime. You can further customize the script to gather additional system information as needed.