My powershell Script Won't Change anything

one of the tasks I want to automate is changing my computer name from an user input. 

$rename = read-host "please enter the computer name:"
write-host "will change to $rename" -foregroundcolor black -backgroundcolor white
$computername = Get-WmiObject Win32_ComputerSystem
$computername.Rename($rename) 
Restart-Computer

This script runs fine but my computer name doesn't change. I'd like to know what I did wrong/

August 21st, 2015 1:24am

Hi Ex Maxina,

make sure the account is permitted to perform this change and try running it elevated.

Also, the method definition of Rename wants three parameters, so maybe this will be more successful:

$computername.Rename($rename, $null, $null)

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 3:00am

hello,

Are you on Powershell version < 3?

if not you could use CmdLet rename-computer

get-help Rename-Computer

Regards.

August 21st, 2015 4:54am

If you have proper permission you can pass user ID and password as arguments of Rename Method

(Get-WmiObject -Class Win32_ComputerSystem).Rename

OverloadDefinitions                                                                                                
-------------------                                                                                                
System.Management.ManagementBaseObject Rename(System.String Name, System.String Password, System.String UserName)

Sample Code

$credential = Get-Credential
Get-WmiObject Win32_ComputerSystem -ComputerName OLDNAME -Authentication 6 |
ForEach-Object {$_.Rename("NEWNAME",$credential.GetNetworkCredential().Password,$credential.Username)}

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 5:24am

Rename-Computer is much easier.

https://technet.microsoft.com/en-us/library/Hh849792(v=wps.620).aspx

August 27th, 2015 4:11am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics