Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-Module ActiveDirectory
- $DaysInactive = 90
- $InactiveDate = (Get-Date).Adddays(-($DaysInactive))
- $amount = Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
- $total = $amount.Lines -1
- $failed = 0
- Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
- write-Host " "
- write-Host "A total of" $total "accounts will be deleted."
- write-Host " "
- $keuze = Read-Host -Prompt "This script will delete Active Directory users. Continue? Y/N"
- $keuze.ToLower()
- if ($keuze -match "y")
- {
- try {
- $User = Get-ADUser -Filter { LastLogonDate -notlike "*" -and Enabled -eq $true } -Properties LastLogonDate | Select-Object -unique @{ Name="Username"; Expression={$_.SamAccountName} }, Name, LastLogonDate
- n
- $Users | Out-File C:\Powershell\deleted.txt -Append
- ForEach ($Item in $Users){
- Remove-ADUser -Identity $Item.DistinguishedName -Confirm:$false
- Write-Output "$($Item.Username) - Account deleted"
- }
- }catch{
- Write-Host " "
- Write-Host "An error occured. Shutting down."
- }
- Write-Host " "
- Write-Host "A total of" $total "accounts have been deleted."
- Write-Host $failed "have not been deleted."
- }else{
- Write-Host "Script closed by user"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement