Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active March 24, 2020 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/7590a0378f99c9c3dea8a430222544f7 to your computer and use it in GitHub Desktop.
Save IISResetMe/7590a0378f99c9c3dea8a430222544f7 to your computer and use it in GitHub Desktop.
function Get-ADLockedOutUser {
[CmdletBinding(DefaultParameterSetName = 'MultiDC')]
param(
[Parameter(Mandatory = $false, ParameterSetName = 'MultiDC')]
[string[]]$DCFilter = @('*'),
[Parameter(Mandatory = $true, ParameterSetName = 'PDCOnly')]
[switch]$PDCOnly
)
$DCs = Get-ADDomainController -Service PrimaryDC -Discover
if($PSCmdlet.ParameterSetName -eq 'MultiDC') {
$DCs = Get-ADDomainController -Filter *|Where-Object {$DC = $_.Name;$DCFilter.Where({$DC -like $_}, 'First')}
}
$lockedout = [System.Collections.Generic.HashSet[guid]]::new()
$DCs.HostName |ForEach-Object{
Write-Progress -Activity "Searching for locked out users" -Status "Querying $_"
Get-ADUser -LDAPFilter '((objectClass=user)(objectCategory=person)(lockoutTime>=1))' -Server $_ |Where {$_.Enabled} |ForEach-Object {
if($lockedout.Add($_.objectGUID)){
$_
}
}
}
Write-Progress -Activity "Searching for locked out users" -Status "Done!" -Completed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment