Rename & disjoin a pc from domain

dear all,
I’m trying to script the procedure of renaming and disjoining a pc from domain.
I used docs.microsoft.com tips and scripts are like below:

Rename-computer –computername $oldname –newname $newname -DomainCredential $ADcred -PassThru -Verbose -Force
Remove-Computer -UnjoinDomainCredential $ADcred -PassThru -Verbose -Force

renaming is done well but in disjoin step there is strange error I couldn’t find out the reason:

failed to unjoin computer ‘pkstest25’ from domain ‘abc.gig’ with the following error message: no mapping between account names and security ids was done.

Would you please give me a hint that what the possible reason is?

Hi, welcome to the forum :wave:

If pkstest25 is the new name of the computer and not the old name then I would hazard a guess that you’re hitting a different domain controller and replication hasn’t completed.

If pkstest25 is the old name of the computer, then I reckon you need to restart the computer after renaming it before you can run Remove-Computer.

A restart and/or waiting for replication to complete will probably fix your problem.