Skip to content
Menu

Join This PC to the AD Domain, Into the Right OU

Joins this PC to an AD domain into the OU you name, with the join account's password read only from a secure RMM field. Dry run by default; real runs need DryRun=false and Approve.

Join this PC to the AD domain, into the right OU is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Join this PC to the AD domain, into the right OU. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+ (not a domain controller); runs as SYSTEM
NeedsPowerShell 5.1, administrator rights, DNS that finds the domain, a join account in a secure RMM field (JoinPassword)
Changes thingsChanges what it runs on

What It Does

Risk is High, so the script lists every change and makes none unless you set DryRun=false and Approve=invoke-domain-join. Typing the script name is the deliberate step. A dry run does not need the password. The script runs on the PC that should join.

It refuses a domain controller. If the PC is already joined to DomainName it ends OK with Already joined and changes nothing. If it is joined to a different domain it refuses, and never moves a PC between domains: remove it from the old one first. It then checks that DNS on this PC can find a domain controller for DomainName, so a PC with the wrong DNS fails early and clearly.

The join uses Add-Computer with the JoinUser account. TargetOu specifies the OU for a new computer account. If it is empty, Active Directory uses the domain's default computer container. DomainController, a full DNS name, can pick the controller. JoinPassword is read only from the environment variable of that name. Make it a secure field in your RMM. It is never printed, logged or stored. Give the join account only the rights it needs for that OU.

After Add-Computer returns, the script checks whether Windows reports the PC as a domain member. If the check fails, it reports an error and asks you to restart and check again. RestartMode Never, the default, leaves the restart to you. IfNoUserSignedIn schedules a restart in 60 seconds only if nobody is signed in. Always schedules it regardless. TargetOu is passed to Windows without being checked first.

Before You Run It

  • Run it on the PC itself, as SYSTEM from your RMM or from an elevated PowerShell.
  • Point the PC's DNS at a domain DNS server first, or the DNS check will stop the run.
  • Create a join account that can only create computer accounts in the target OU, and put its password in a secure RMM field named JoinPassword.
  • Do a dry run first, then run again with DryRun=false and Approve=invoke-domain-join. Warn anyone using the PC before choosing a restart mode.

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-domain-join, 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: Joins the PC to the domain (creates its computer account) and can restart it.
  • Restart: It restarts the machine itself: that is its job, or an input you set turns it on. The answer says when.
  • Typical run: Under 1 minute.
  • Example answer: "Joined contoso.com in OU=Workstations,DC=contoso,DC=com; restart needed to finish"
  • 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.

  • DomainName: The domain's DNS name, e.g. contoso.com. Required.
  • TargetOu: The OU for the computer account, as a distinguished name, e.g. "OU=Workstations,DC=contoso,DC=com". Default empty: the default Computers container.
  • JoinUser: The account that joins the PC, DOMAIN\user or user@domain. Required for a real run.
  • DomainController: Full DNS name of the domain controller to join through, e.g. dc1.contoso.com. Default empty: Windows chooses.
  • RestartMode: Never (default), IfNoUserSignedIn or Always.

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.

The Script

invoke-domain-join.ps1 · 862 lines · 45.1 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.