This script enables to rename a host with its serial number automatically.
It does detect if the computer is part of a domain environment and use the appropriate switch to pass the admin account (if provided) on execution.
This script is meant to be run in a Windows environment due to the use of Windows Management Interface calls. Although, it can be updated to use a more universal way to obtain a computer’s serial number.
The kind of use case this script solves is an environment where a Windows installation or image may be reproduced onto other machines. This can be due to having a GM image that’s deployed on new stations or having a older station being cloned to another, newer one.
The following software is required in order to run this script.
Name | Version | Description | Use |
---|---|---|---|
Windows PowerShell | 5+ | Scripting and command line environment. | Scripts execution. |
Name | Type | Required | Description | Default value |
---|---|---|---|---|
ComputerName | string |
☑️ | Overrides the computer name to apply. | (Get-WmiObject Win32_BIOS).SerialNumber |
AdminAccount | string |
⬜ | Specifies the administrative account username to use for performing the host name change. | |
Restart | switch |
⬜ | Asks to restart the computer after renaming it. | $false |
ForceSync | switch |
⬜ | Forces to rename the computer even if the hostname matches the desired computer name. | $false |
This method works for enabling a manual way to admins to refresh the hostname of a computer.
Create a shortcut with the following details :
Target
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments
-NoProfile -NoLogo -ExecutionPolicy Bypass -File "\\Contoso.com\SYSVOL\Contoso.com\Scripts\Sync-HostnameWithSerial.ps1" -AdminAccount %UserName% -Restart -ForceSync
This method works as an automated way to refresh the hostname of a computer if it doesn’t match the desired computer name upon start up. It is meant to be run under the computer’s account’s context.
Create a shortcut (Computer Configuration ➡️ Preferences ➡️ Windows Settings ➡️ Shortcuts) and fill in the following details :
Page | Property | Value |
---|---|---|
General | Action | Replace |
General | Name | (Desired shortcut name) |
General | Target type | File System Object |
General | Location | StartUp |
General | Target path | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe |
General | Arguments | -NoProfile -NoLogo -ExecutionPolicy Bypass -File "\\Contoso.com\SYSVOL\Contoso.com\Scripts\Sync-HostnameWithSerial.ps1" -Restart |
General | Start in | \\Contoso.com\SYSVOL\Contoso.com\Scripts\ |
Common | Remove this item when it is no longer applied | ☑️ |
This method works as an automated way to deploy a handy shortcut for admins to use when they need to force syncing the computer name manually.
Create a shortcut (User Configuration ➡️ Preferences ➡️ Windows Settings ➡️ Shortcuts) and fill in the following details :
Page | Property | Value |
---|---|---|
General | Action | Replace |
General | Name | (Desired shortcut name) |
General | Target type | File System Object |
General | Location | Start Menu |
General | Target path | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe |
General | Arguments | -NoProfile -NoLogo -ExecutionPolicy Bypass -File "\\Contoso.com\SYSVOL\Contoso.com\Scripts\Sync-HostnameWithSerial.ps1" -Restart -ForceSync |
General | Start in | \\Contoso.com\SYSVOL\Contoso.com\Scripts\ |
Common | Run in logged-on user’s security context | ☑️ |
Common | Remove this item when it is no longer applied | ☑️ |