0

I'm currently creating a PowerShell script who need to be able to rename computer name:

Write-Host "Enter the new computer name: " -NoNewline
$name = Read-Host
Rename-Computer -NewName $name

And when I execute this code I got this error, even if I start the script with Administrator rights, I get the same error.

Rename-Computer : The computer "LAPTOP-ASVDNCPD" could not be renamed to "[new_computer_name]" because of the following exception: Access Denied.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Is the computer domain joined? – henrycarteruk Sep 29 '17 at 15:38
  • No, the computer is not into a domain. And I don't want to join a domain. – Aurèle Bonvin Sep 29 '17 at 15:41
  • 1
    Try adding a `-LocalCredential Srvname\Administrator -Force -PassThru` into the same command; it should work! Check here for more info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-computer?view=powershell-5.1 – Am_I_Helpful Sep 29 '17 at 15:43
  • 2
    Is the account you're running the script as (likely that you're logged on with) a member of the Administrators group? You will also need to elevate PowerShell ('Run as Administrator'). – henrycarteruk Sep 29 '17 at 15:44
  • @JamesC. Yes, it is in Administrators group. But after, I want to run the script by righ clicking and "Execute with PowerShell" (or by running bat file) – Aurèle Bonvin Sep 29 '17 at 15:49
  • 2
    You'll need to self elevate the script then: [PowerShell: Running a command as Administrator](//stackoverflow.com/q/7690994) – henrycarteruk Sep 29 '17 at 16:05

1 Answers1

0

You need to self elevate the script to run the command as Administrator. PowerShell: Running a command as Administrator