Rename the Computer Name

Please use the script to rename the computer if the computer even in domain

NewCompName = 'ComputerNoSpaceName' ## Enter your computer name with out any space to change the target computer
## Function to Execute CMD through Subprocess Module
def ExecuteCMD(CMD, OUT = False):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:

    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    RET = OBJ.returncode
    if RET == 0:
        if OUT == True:
            if out != '':
                return out.strip()
                return True
            return True
        return False

OldCompName = ExecuteCMD('hostname', True)
DisString = ''
##print ExecuteCMD('WMIC ComputerSystem where Name="'+OldCompName+'" call Rename Name="'+NewCompName+'"', True)
if ExecuteCMD('WMIC ComputerSystem where Name="'+OldCompName+'" call Rename Name="'+NewCompName+'"'):
    DisString = 'Success: the Computer has been renamed as "'+NewCompName+'" from the old name "'+OldCompName+'"'
    ExecuteCMD('shutdown -r -t 00')

if DisString:
    print DisString

Sample Output:

Export Script Here,

20170124-Rename-the-Computer-Name.json (2.33 KB)

Rename the Computer Name.png

Can this be written in a way that it will ask for a parameter (Comp Name) when executing it rather than modifying the script every time it is used. We have several computers that need renamed, and it would be easier to be able to just enter the desired name at execution time. A prompt for a new name would be nice.

Thank you

@Dave_Kotula ,

We have conferred with our Script Developers if such task can be modified. We’ll provide their feedback as soon as possible.

Thank you!

when i tried it get restart but not changing hostname

Hi @Thoha,

May I know whether you tried the script from here

Kind Regards,