1
$changename=$pcname.text
function namechange {
     Rename-Computer -NewName "$pcname.text" -Restart    
}

$pcname.text is a textbox where I manually put the PC name, after that I click a button so the nameChange starts. The computer isn't in a domain and the I'm logged in as a local admin. When I run my code it still returns "Access denied". When I google the Rename-Computer command it says I should be able to edit it just like the above code.

What did I do wrong? Can someone help me with some proper code or fix the above?

Palle Due
  • 5,929
  • 4
  • 17
  • 32
  • As with your [Changing ownership tag with powershell](https://stackoverflow.com/q/60849124/1701026) BIOS question, you will need to run this as an administrator with elevate rights, see: [Running a command as Administrator using PowerShell?](https://stackoverflow.com/q/7690994/1701026) – iRon Mar 26 '20 at 09:08
  • The script that I run has the following code so it runs as administrator: Start-Process powershell -verb runas -ArgumentList "-file fullpathofthescript" I am also logged in as an local administrator but it still says access denied. – Cas van den Wijngaard Mar 26 '20 at 09:16
  • If your computer is connected to a domain, you will need to supply `-DomainCredential` and `-LocalCredential`, see: [`Rename-Computer`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/rename-computer?view=powershell-7) – iRon Mar 26 '20 at 09:20
  • The computers I want to change the name aren't connected to a domain yet so i don't need to use `-DomainCredential`. I am logged in as an local admin and run the script as admin so do i need to use `-LocalCredential`. – Cas van den Wijngaard Mar 26 '20 at 09:25
  • `"$pcname.text"` suggests you are trying to build a GUI, test the command directly from an (elevated) PowerShell prompt. – iRon Mar 26 '20 at 09:27
  • 1
    Remove the quotes around `$pcname.text` or use `"$($pcname.text)"` – Theo Mar 26 '20 at 13:56
  • Whenever I do this in an elevated Powershell prompt it runs without any problems but when I try to use it within my gui it gives my a access denied error. – Cas van den Wijngaard Mar 30 '20 at 09:27

0 Answers0