PowerShell Hostname

The PowerShell can be used to find out the hostname or computer name and also to change it.

Advertisment

Show hostname

The PowerShell can be used to display the hostname of the device. This is useful for e.g. the administration of a device. Here it can be ensured that it is the device that is to be administered in the corresponding case.

To do this, PowerShell is opened and the command “hostname” is entered.

PowerShell Hostname
Hostname in PowerShell

Furthermore, PowerShell can also query environment variables. To get the hostname with this, the following command:

$env:computername

PowerShell Hostname Alternative
Hostname in PowerShell

Advertisment

Change hostname

The hostname can also be changed accordingly. This is useful, for example, in managed environments to live a consistent naming concept. This can simplify the administration, especially in larger environments. Here the appropriate syntax is crucial. This can consist, for example, of the location, a sequential number, separation between notebook and computer or also serial number.

According to Microsoft the maximum length of the host name is 15 characters as of 20.07.2023. This should be taken into account when creating your own syntax.

In theory, the computer can also be addressed via DNS using a longer name. However, problems can then occur when working with certificates. Since the certificates are issued on the computer name and not on the DNS of the device.

To change the computer name with PowerShell, the following command is used.

Rename-Computer -NewName <“New Computername“>

The Rename-Computer function can also be used to rename a remote computer, for example. This works, for example, in a domain, with a user who has administrator privileges to rename the devices.

Rename-Computer -ComputerName <“Current Computername“> -NewName <“New Computername“ -DomainCredential <Domain>\<Administratoruser> -Force

The Force parameter suppresses the confirmation prompt. This can also be omitted under certain circumstances.

Since the new host name or computer name only takes effect after a restart, this can also be specified as a parameter in the command.

Then, for example, the command would read as follows:

For the local computer

Rename-Computer -NewName <“New Computername“> -Restart

For a remote computer in a domain

Rename-Computer -ComputerName <“Current Computername“> -NewName <“New Computername“ -DomainCredential <Domain>\<Administratoruser> -Force -Restart

Advertisment

Scroll to Top