Forum Discussion

dannytveria's avatar
dannytveria
Brass Contributor
Oct 26, 2021
Solved

Disable \ Remove old computer accounts

Hi, I have a script for disabling & removing old computer accounts.   Import-Module ActiveDirectory # Set the Parameters since last logon $ForDisable = "C:\Logs\Computers Accounts\Disable-$((Ge...
  • AharonBensadoun's avatar
    AharonBensadoun
    Nov 02, 2021

    dannytveria 

     

    Ok , test it and let me know if this help:

    $Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=staff ,DC=local" | Where-Object {($_.Enabled -eq $true) -and ($_.distinguishedname -notlike "*,OU=Servers,*")  -and ($_.distinguishedname -notlike "*,OU=Test,*") -and ($_.distinguishedname -notlike "*,OU=IT,*") -and ($_.distinguishedname -notlike "*,OU=Laptops,*") -and ($_.distinguishedname -notlike "*,CN=Computers,*") -and ($_.distinguishedname -notlike "*,CN=Managed Service Accounts,*") }

Resources