What It Does
This script is a dry run until you set DryRun=false and Approve=invoke-pc-retirement, and it refuses more changes than MaxChanges, 25 by default, in one run. It is for Windows 10 and 11 PCs; servers and domain controllers are skipped. Each part that does not apply, because the PC is not joined or the input turns it off, is skipped with its reason in the table.
Entra ID: if the PC is Entra joined or hybrid joined, it runs dsregcmd /leave. MDM: any remaining enrollment, such as Intune, is removed from this PC: the registry key, exported to the backup folder first, and its scheduled tasks. Domain: if the PC is joined to a domain, it leaves it and joins the workgroup WorkgroupName; the PC must be restarted to finish this, and the script never restarts it. LeaveEntra, LeaveDomain and UnenrollMdm turn each part off.
It writes a retirement record, a text file in the Retirement folder under the MSPOutpost data folder, with the computer name, serial number, make and model, operating system, disk models and serial numbers, BitLocker status and recovery key IDs (never the keys), what the PC was joined to, what this run did and the Note you give. A PC that is not joined to anything still gets a record if one does not exist. If there is an existing record and no applicable changes, it keeps that record.
It does not wipe anything. User files, profiles and programs are untouched, and so are cached credentials and saved passwords. Erase the drives before the PC leaves your control. It leaves the computer account in Active Directory and the device record in the MDM console, so delete those there. It does not remove the RMM agent that is running it, because that would end the run: use uninstall-rmm-agent last, once the record is written. After a domain leave, restart and run it again to confirm; a second run with nothing left to leave says Already retired.
Before You Run It
- Run it as a dry run first, on the PC, and read the table. It lists what it would leave and remove and writes nothing.
- Read the dry run with care on a joined PC. The Entra, domain and MDM parts need a PC joined to a real tenant or domain to see them work.
- Decide each part. Set LeaveEntra, LeaveDomain or UnenrollMdm to false for a part you want to leave in place.
- Keep the BitLocker recovery key somewhere safe if the drive is encrypted. The record holds only the key IDs.
- Afterwards delete the device in the MDM, Entra ID and Active Directory consoles, erase the drives, and remove the RMM agent last with uninstall-rmm-agent.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=invoke-pc-retirement, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Leaves Entra ID and the domain, removes MDM enrollments (registry key and tasks), writes a retirement record file; does not wipe or restart.
- Restart: It never restarts the machine itself. When a restart is needed to finish, the answer says so.
- Typical run: Under a minute.
- Example answer: "Retired: left Entra ID, left domain corp.local (restart to finish); record PC042-20261117-101500.txt written; disks NOT wiped"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- LeaveEntra: true leaves Entra ID (dsregcmd /leave) when the PC is joined or registered. Default true.
- LeaveDomain: true leaves the Active Directory domain and joins WorkgroupName. Default true.
- UnenrollMdm: true removes the MDM enrollment from this PC. Default true.
- WorkgroupName: The workgroup to join when leaving the domain. 1 to 15 letters, digits or hyphens. Default WORKGROUP.
- Note: Free text for the record, for example a ticket number or the reason. Up to 100 characters, plain ASCII. Default empty.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.