Friday, December 6, 2024
HomePowershellThe right way to Get the Laptop Identify with PowerShell — LazyAdmin

The right way to Get the Laptop Identify with PowerShell — LazyAdmin


PowerShell has plenty of cmdlets, however unusual sufficient probably not one which returns solely the computername. However that doesn’t imply we will’t get the title of the system, there are literally a number of choices to return the pc title, even with a single command.

In case you’re questioning why you wish to look it up, I usually use the pc title as a part of a log file title for automated scripts. Or generally you simply wish to shortly see which machine you’re engaged on.

On this article, I’ll present you the completely different strategies to get the pc title from the native machine and distant computer systems.

Get Laptop Identify

The best technique to get the title of the pc that you’re at present engaged on is definitely to make use of the outdated command hostname. This command is often used within the command immediate, nevertheless it additionally works completely in PowerShell

get computer name powershell using hostname

We will merely retailer the hostname in a variable to make use of later in a script. It’s additionally attainable to pipe different cmdlets behind it, like clip to repeat to the clipboard, or one thing else.

# Retailer hostname in variable
$hostname = hostname

# Copy hostname to clipboard
hostname | clip

Utilizing the Atmosphere Variable

A extra PowerShell approach to retrieve the hostname is to make use of the setting variable. It is a set of dynamic variables which is utilized by the working system and different processes to retailer data. We will entry the data saved within the setting variable by utilizing the $Env: drive, adopted by the variable title.

So to get the pc title, we will do the next:

$env:COMPUTERNAME

The issue nevertheless with this technique, is it solely works on Home windows machines. Generally that’s adequate, but when it is advisable to run your script on macOS as effectively, then a greater choice is to make use of the Atmosphere class to get the machine title. This works on all machines:

[Environment]::MachineName

Utilizing the Frequent Data Mannequin (CIM)

One other quick and dependable technique to retrieve the title of the system is to make use of the Frequent Data Mannequin (CIM). That is the successor of the older, well-known, WMI object that we beforehand used to get details about the pc.

The benefit of utilizing Get-CimInstance is that we will additionally use this technique for distant computer systems.

(Get-CimInstance -ClassName Win32_ComputerSystem).Identify

Get a Distant Laptop Identify

In some instances, it is advisable to get the pc title of a distant machine. We might use any of the strategies above and easily place them within the scriptblock of the invoke-command cmdlet, like this:

Invoke-Command -ComputerName "192.168.1.12", "192.168.1.14" -ScriptBlock { $env:COMPUTERNAME }

However on this case, a greater choice is to see the Get-CimInstance cmdlet. That is sooner and lets you get the pc title of a number of distant computer systems:

Get-CimInstance -ComputerName "192.168.1.12", "192.168.1.14" -ClassName Win32_ComputerSystem| Choose PSComputerName,Identify

Utilizing Resolve DnsName

One other technique to get the pc title primarily based on the IP Handle is to make use of the Resolve-DnsName cmdlet. This command will question the DNS server to resolve the IP Handle of the pc.

Resolve-DnsName -Identify 192.168.1.12 | Choose-Object -ExpandProperty NameHost

Wrapping Up

There are extra methods to search for the title of a tool, however most of them usually are not actually handy, and even slower (like Get-ComputerInfo). On an area machine is the hostname command the simplest one to recollect and use.

If it is advisable to search for the title inside a script, then I’d suggest utilizing the setting variable or class relying on the working methods in your setting.

You probably have any questions, simply drop a remark under.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments