Introduction
A very tedious task I find is renaming your PC. The GUI is constantly changing, especially as we move towards newer versions of Windows. It's far easier to rename your computer through PowerShell than trying to keep up with the GUI changes. It is important to name your computers properly as when troubleshooting logs or working with other services, the right name can speed up the resolution progress quicker. But how can you rename a PC in PowerShell?
Quick Code
Expected input
Rename-Computer -NewName "NewHostName" -Restart
Background - Renaming a PC
There are multiple ways to change the hostname of a Windows server. It is important to cover the different ways you can not only rename a server but also validate the current name. When you write a script, it is important to always validate your current code rather it's the results or a dry run, all code should be validated before executing.
How To Check the Current Hostname
Before you can rename a server, you should confirm the current hostname. We have multiple ways to validate the current name. As you write scripts, security may change what code can be executed or you might find that other code is deprecated. It is important you focus your ability to learn different ways you can query system information.
Additionally, you want to use variables that can query the system to ensure your code can be executed on any system without the requirements of parameters or hard codes. We will also find that as we run the following code to pull hostnames, it will be quicker and give us the ability to write better code in the future.
How to Check Using the Hostname Command
Windows has an older way of pulling the hostname from the command line. This command also works when running Windows PowerShell. You can pull the value of the hostname by running the command, hostname.
Input code
hostname
Output code
DESKTOP-T2GC86J
ScreenShot
How to Check Using Environment Variables
When querying the hostname, you can take advantage of Environment Variables. These variables are configured through the system and can be queried directly from the PowerShell console. The variable you use is $env:COMPUTERNAME. You can coconfirmnfrim this variable is an Environment Variable by the pre-fix, $env:.
Input code
$env:COMPUTERNAME
Output code
DESKTOP-T2GC86J
ScreenShot
How To Change the Hostname
Time to break out the code that changes the current hostname. Since we understand the code to validate hostnames without depending on the slow GUI, we can move forward with making changes to the system. There are multiple ways to change the hostname that range from local management or remote management. If you do not have Active Directory configured, along with DNS, remote hostname changes may fail.
How to Change the Hostname Locally with Automatic Restart
Similar to changing the hostname through the GUI, a system restart is required, even from PowerShell. The requirement for the restart is tied to services and registry values that must be updated, along with other system settings.
To make the hostname change, simply execute the following code. But as you run this code, think about how you can use this code to automate future tasks such as renaming multiple servers directly from a CSV file.
Input code
Rename-Computer -NewName "NewHostName" -Restart
How to Change the Hostname Locally without Automatic Restart
As you are aware, you can not always restart a computer on the spot but you might want to pre-stage a name change. In order to perform this action, simply remote the -Restart parameter from your code.
Input code
Rename-Computer -NewName "NewHostName" -Restart
How to Change the Hostname Remotely
With the use of other cmdlets like invoke-command, you can provide solutions to take Rename-Computer and use it on remote machines. But Rename-Computer has the feature built-in. If you want to rename a server using the remote function and you have the machine connected to your domain, you can execute the following code.
Rename-Computer -ComputerName "CurrentHostName" -NewName "NewHostName" -DomainCredential DomainName\AdminName -Force
Conclusion
Regardless of how you change your hostname, the process is clean and simple. If you have any feedback or know a better way to manage hostnames, reach out and comment below. Code only improves with feedback.
This! This is a very well produced and written article! I can’t wait to read your next articles.