Disable-ADAccountGui.ps1 947 B

123456789101112131415161718192021222324252627282930313233343536
  1. param(
  2. [hashtable]$GetADUserArgs=@{},
  3. $Where={$true}
  4. )
  5. if (-not $GetADUserArgs.Properties) {
  6. $GetADUserArgs["Properties"] = @(
  7. "mail",
  8. "lastLogon",
  9. "lastLogonTimestamp",
  10. "distinguishedName",
  11. "UserPrincipalName"
  12. )
  13. }
  14. if (-not $GetADUserArgs.Filter) {
  15. $GetADUserArgs["Filter"] = {Enabled -eq $true}
  16. }
  17. $GetADUserArgs | Format-Table | Out-Host
  18. $users = Get-ADUser @GetADUserArgs | Where-Object $Where | Sort-Object lastLogonTimestamp | Foreach-Object {
  19. [PSCustomObject]@{
  20. dn=$_.distinguishedName
  21. UserPrincipalName=$_.UserPrincipalName
  22. mail=$_.mail
  23. lastLogonTimestampStr=[datetime]::FromFileTimeUtc($_.lastLogonTimestamp)
  24. lastLogon=$_.lastLogon
  25. lastLogonTimestamp=$_.lastLogonTimestamp
  26. }
  27. } | Out-GridView -OutputMode Multiple
  28. $users | Foreach-Object {
  29. ([string]$_.dn) | Out-Host
  30. Disable-ADAccount -Identity ([string]$_.dn)
  31. }