PowerShell Script doesn’t change computer name

Tags:
Batch Scripts
PowerShell Scripts
Trying to change computer name with a PowerShell script. In order to make things easier I included a batch file so I can double click the batch file and it runs the PowerShell Script.
//batch file
@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
//PowerShell Script
If(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator"))
{
Echo "This Script needs to be run as Admin"
Break
}
else
{
Get-ExecutionPolicy
$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)
Get-ExecutionPolicy
Restart-Computer
}
The idea is that I double click the batch file, it opens the PowerShell with Administration privileges and my Script changes the computername to a name of my choice. My problem is that it runs perfectly fine but the script doesn't change the computer name. I ran the same PowerShell commands in the CLI and it works but when I run the script it doesn't. There are no error messages.
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: