Advertisement
Guest User

Untitled

a guest
Apr 5th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Import-Module ActiveDirectory
  2.  
  3. $DaysInactive = 90
  4. $InactiveDate = (Get-Date).Adddays(-($DaysInactive))
  5.  
  6. $amount = Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
  7.  
  8. $total = $amount.Lines -1
  9. $failed = 0
  10.  
  11. Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
  12.  
  13. write-Host " "
  14.  
  15. write-Host "A total of" $total "accounts will be deleted."
  16. write-Host " "
  17.  
  18. $keuze = Read-Host -Prompt "This script will delete Active Directory users. Continue? Y/N"
  19. $keuze.ToLower()
  20.  
  21. if ($keuze -match "y")
  22.  
  23. {
  24. try {
  25. $User = Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
  26. n
  27.  
  28. $Users | Out-File C:\Powershell\deleted.txt -Append
  29.  
  30.  
  31.  
  32. ForEach ($Item in $Users){
  33. Remove-ADUser -Identity $Item.DistinguishedName -Confirm:$false
  34. Write-Output "$($Item.Username) - Account deleted"
  35. }
  36.  
  37.  
  38. }catch{
  39. Write-Host " "
  40. Write-Host "An error occured. Shutting down."
  41. }
  42.  
  43.  
  44. Write-Host " "
  45. Write-Host "A total of" $total "accounts have been deleted."
  46. Write-Host $failed "have not been deleted."
  47.  
  48.  
  49. }else{
  50. Write-Host "Script closed by user"
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement