Powershell change hostname in a workstation or member server joined to the domain.
Hi, I use the powershell to change a machine name, I use $name as variable to input a name and the follow cmd-let: $computerName.Rename($name) but it only work in a unjoined domain machine.
my script is the following:
$computerName = Get-WmiObject Win32_ComputerSystem
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Desired Computer Name ")
$computername.rename("$name")
but I need to change the hostname of any computer joined in my domain, may be it's another command, I don't know and I can't find information seeking the web.
I don't need a remote procedure, only a local to facilitate to my servicedesk to change hostnames in different machines.
If somebody knows something I'll be pleasure to know it.
January 17th, 2014 9:56am
Hello.
Look at Rename-Computer cmdlet.
Run this to get help:
get-help Rename-Computer -full
You can enumerate all "Computer" cmdlets using this command:
get-command -Noun Computer
Good luck)
- Edited by
Kolomiets Sergei
19 hours 49 minutes ago
- Proposed as answer by
Mike Laughlin
14 hours 37 minutes ago
January 17th, 2014 10:20am
Hi Kolomiets, I undestand that Rename-Computer is a remote cmdlet, I need to do it localy. I'll test the get-command in monday. Thanks.
January 17th, 2014 12:34pm
Hello.
Look at Rename-Computer cmdlet.
Run this to get help:
get-help Rename-Computer -full
You can enumerate all "Computer" cmdlets using this command:
get-command -Noun Computer
Good luck)
- Edited by
Kolomiets Sergei
Friday, January 17, 2014 3:43 PM
- Proposed as answer by
Mike Laughlin
Friday, January 17, 2014 8:55 PM
January 17th, 2014 6:14pm
My version of PS is 2.0, I see that this cmdlet is only in 3.0 and up. another way?
January 18th, 2014 3:14pm
Hello.
I will test your code on domain machine on Monday. Which OS has your computer?
Did you restart computer after renaming?
If calling of WMI method doesn't work, then you can try this command:
netdom renamecomputer /?
Good luck)
January 18th, 2014 5:15pm
My system is win 7 sp1, I rebooted pc and nothing, the script only work with stand alone pc, I'll tray with netdom, may be the solution, I hope, thanks
January 19th, 2014 7:20am
Can you post the output of this code:
$name=read-host "Enter new name"
$computerName = Get-WmiObject Win32_ComputerSystem
$computername.rename("$name") | select -expand ReturnValue
I`m interesting in ReturnValue.
January 19th, 2014 11:13am
Hi kolomiets, I forget that netdom is a server command, then it's a remote command to rename a PC, I wish a local command to do a script that pront a helpdesk user asking for the Hostname in a joined machine.
January 20th, 2014 5:02am
this script is the same that mine, it don't return error but in Computer Properties near the Hostname must say, in the next reboot the name will change, or something like that, and nothing, and even I reboot the system to see if for a miracle it change
the hostname and nothing.
January 20th, 2014 5:09am
I`ve tested your code on computer which is joined to domain and have Windows 8 installed (unfortunately I don`t have Windows 7 in my test environment). The WMI method rename() is working fine:
PS C:\Users\administrator.BRIDGESTOUN> $name=read-host "Enter new name"
Enter new name: uk-hq-w8
PS C:\Users\administrator.BRIDGESTOUN> $computerName = Get-WmiObject Win32_ComputerSystem
PS C:\Users\administrator.BRIDGESTOUN> $computername.rename("$name") | select -expand ReturnValue
0
PS C:\Users\administrator.BRIDGESTOUN> restart-computer
The exit code is 0 (Operation is successful). After reboot my station has had new name. I have domain controller on Windows Server 2008 R2.
Windows 8 doesn`t have netdom utility and I couldn`t test it, but as I remember this command should rename local computer (replace username and run from powershell):
$newname=read-host "Enter new name"
netdom renamecomputer $env:computername /NewName:$newname /UserD:DOMAIN\someuser /PasswordD:* /SecurePasswordPrompt /REBoot
Maybe in your case you don`t have necessary rights in domain. Anyway only return code can give an answer.
January 20th, 2014 6:05am