This Powershell script will use nslookup to query a DNS Server to get the hostname and use that result to set the computer name.
If you use this you will need to amend as necessary including the address of the DNS Server (I have added the Google one here for illustration) and I am filtering to ensure I get a single IP address on my machine starting with 123.* (again for illustration).
You may first need to allow execution of unsigned scripts:
# Set the Powershell Execution Policy as this script is unsigned & machine not yet on domain Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force
Then run the script:
# First get the IP address of the local machine $ipAddresses = (get-netadapter | get-netipaddress | ? addressfamily -eq 'IPv4').ipaddress # Filter out any 192.168 172. 10. IP addresses $ipAddress = $ipAddresses -like "123*" # Retrieve the hostname from the ??? DNS Server $fqdn = (nslookup $ipAddress 184.108.40.206 | sls name | select -last 1).toString().split(":").trim() # We only need the hostname without the domain info so split it up $fqdn_items = $fqdn.split(".") $newComputerName = $fqdn_items Write-Host "New Computer Name: $newComputerName" # Get a WMI object representing the current computer $currentComputer = Get-WmiObject Win32_ComputerSystem Write-Host "Attempting to change computer name to $newComputerName" # Set the Computer Name to the hostname found via DNS Lookup to DNS Server # This can only be performed before joining the domain otherwise you get return code 1326 $currentComputer.Rename($newComputerName)