DEV Community

Anthony Buhnerkemper
Anthony Buhnerkemper

Posted on

Hybrid Active Directory with PowerShell 7: Stale Accounts, Replication and Entra Connect Sync

I have worked in IT for about 20 years. A lot of that was MSP operations. Today I work as an M365 cloud admin.

Most tenants I have seen are still hybrid. Users live in on-prem Active Directory and sync to Entra ID. So the cloud is only as clean as AD.

I kept writing the same AD and sync checks. So I cleaned them up and shared them on GitHub. This toolkit uses PowerShell 7, the RSAT ActiveDirectory module, the ADSync module and Microsoft Graph.

Repo: active-directory-hybrid-powershell-toolkit

The problem it solves

Old accounts pile up in AD. Nobody is sure who is really in Domain Admins.

Sync breaks quietly. A duplicate email alias blocks one user, and you find out when they call.

These scripts make those checks quick and repeatable.

Highlight scripts

Get-ADStaleAccountReport.ps1

Finds enabled users and computers that have not logged on in a number of days. You can skip OUs like service accounts.

./Get-ADStaleAccountReport.ps1 -DaysInactive 90 -ExcludeOU 'OU=Service Accounts,DC=contoso,DC=com'
Enter fullscreen mode Exit fullscreen mode

Disable-ADStaleAccount.ps1

Takes the reviewed CSV and disables each account. It stamps the description and can move it to a holding OU. Always run with -WhatIf first.

./Disable-ADStaleAccount.ps1 -CsvPath ./reviewed.csv -TargetOU 'OU=Disabled,DC=contoso,DC=com' -WhatIf
Enter fullscreen mode Exit fullscreen mode

Get-ADPrivilegedGroupReport.ps1

Expands nested members of Domain Admins and the other built-in admin groups. It flags stale admins and passwords that never expire.

./Get-ADPrivilegedGroupReport.ps1
Enter fullscreen mode Exit fullscreen mode

Compare-ADEntraUser.ps1

Matches AD users to their Entra ID accounts. It reports users that are not synced, UPNs that do not match, and enabled status that does not match.

./Compare-ADEntraUser.ps1
Enter fullscreen mode Exit fullscreen mode

Other scripts cover password policy, replication health, Entra Connect sync status, starting a delta sync, sync errors and UPN readiness before you sync a new OU.

How the scripts are built

  • PowerShell 7.4 or later.
  • Comment-based help on every script. Run Get-Help ./script.ps1 -Full.
  • Reports export to CSV.
  • Change scripts support -WhatIf and -Confirm.
  • Certificate (app-only) or interactive sign-in for Graph.
  • No secrets in the repo. Examples use contoso.com and placeholder GUIDs.

Please test in a lab domain and a test tenant first. Read each script before you run it.

My usual workflow

I start with the reports. They are read-only and safe. I open the CSV in Excel and look for anything odd.

Then I pick one fix. I run the change script with -WhatIf and read the output. If it looks right, I run it again for real.

One hybrid tip. If you move a disabled user out of your sync scope, the cloud account gets soft-deleted on the next sync. Plan for that.

Getting started

Clone the repo:

git clone https://github.com/anthonybuhnerkemper/active-directory-hybrid-powershell-toolkit.git
cd active-directory-hybrid-powershell-toolkit
Enter fullscreen mode Exit fullscreen mode

The AD scripts only need RSAT. For the cloud side, every toolkit ships the same Connect-M365Admin script. Dot-source it, then connect.

# Interactive sign-in
. ./scripts/Connect/Connect-M365Admin.ps1
Connect-M365Admin -Service Graph -Interactive -GraphScopes Organization.Read.All, Directory.Read.All, User.Read.All

# Or app-only with a certificate
$env:M365_TENANT_ID       = '00000000-0000-0000-0000-000000000000'
$env:M365_CLIENT_ID       = '00000000-0000-0000-0000-000000000000'
$env:M365_CERT_THUMBPRINT = '0000000000000000000000000000000000000000'
Connect-M365Admin -Service Graph
Enter fullscreen mode Exit fullscreen mode

Run a report, check the CSV, then call Disconnect-M365Admin when you are done. The README lists the modules and rights each script needs.

More toolkits

I have more free toolkits that use the same pattern, including entra-id-powershell-toolkit, exchange-online-powershell-toolkit, intune-powershell-toolkit and defender-xdr-powershell-toolkit.

You can find them all on my GitHub profile.

Feedback welcome

Issues, ideas and pull requests are welcome. What would you add? Tell me in the comments.

Top comments (0)