I am trying to create a FAST API to change the name of the system by executing the bat files. The name gets changed but its not accepting special characters like -, , and '.

I have searched a lot but didn't get any solution for it. I have tried executing the PowerShell command but it asks for admin access.

Here is my bat file code:

### For Admin Previleges
set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

set arg1=%1
set arg2=%2

wmic computersystem where caption='%arg1%' rename '%arg2%'

move nul 2>&0

Here is my python code for executing batch files:

def set_Computer_Name(computer_name : str):
    current_computer_name = platform.node()
    process = subprocess.run(['computer_name.bat', current_computer_name, computer_name])
    return {"status" : process.returncode}

How can I fix this?


1 Answer 1


If you elevate/run-as-admin your python process first, then your subprocess will also be elevated. Then you can run Rename-Computer normally instead of trying to manage special characters through three languages:

# python running as admin
process = subprocess.run(['powershell.exe', 'Rename-Computer', computer_name])

If you can't elevate python for some reason, you can use powershell instead of batch to elevate its own child process:

process = subprocess.run([
  'Start-Process powershell.exe -Verb runas', 
  '-ArgumentList { Rename-Computer -Newname', computer_name, '}'

And to be clear about special characters, windows computer names must be characters a-zA-Z, and may include 0-9 or -. Characters like ' or , are not allowed.

One exception to basic elevation happens when the computer is joined to a windows domain. The user must have both local administrator rights, and rights to the computer object in AD. It's common to elevate using just a local admin user, so I mention it here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .