Forum Discussion
dannytveria
Aug 26, 2021Brass Contributor
Disable and removal of Computer accounts
Hi, I made a script to disable old computer accounts. My Sysadmin asked me to disable after 180 days and remove them after a year. My goal is to disable computer accounts after 180 days and export...
- Sep 13, 2021
dannytveria
Aug 27, 2021Brass Contributor
Yes,
I trying to disable the computer accounts that have not been logged for 180 days, and export them on CSV file.
Also, I want to delete the computer's accounts that have not been logged for 365 days, and export them as well.
I trying to disable the computer accounts that have not been logged for 180 days, and export them on CSV file.
Also, I want to delete the computer's accounts that have not been logged for 365 days, and export them as well.
farismalaeb
Aug 28, 2021Iron Contributor
I wrote a quick script to do what you need, but please note
this script will REMOVE, and DISABLE adcomputer account, test it first and make sure its doing the result you need before applying it to production..
So what you need to change only is the last line,
Get-BadPC -Days 180 -Action Disable
Days= what ever number of day
and action if you want to delete or disable.
function Get-BadPC{
param(
[parameter(mandatory=$true)]$Days,
[parameter(mandatory=$true)]
[ValidateSet('Delete','Disable')]$Action
)
$InactiveDate = (Get-Date).Adddays(-($Days))
$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=Test ,DC=local" | Where-Object {($_.distinguishedname -notlike "*,OU=Servers,*") -or ($_.distinguishedname -notlike "*,OU=Test,*") -or ($_.distinguishedname -notlike "*,OU=IT,*") -or ($_.distinguishedname -notlike "*,OU=Laptops,*") -or ($_.distinguishedname -notlike "*,CN=Computers,*")}
$Computers_For_Action | Export-Csv "C:\Users\Administrator\Downloads\$($Action)-$((Get-Date).ToString('dd-MM-yyyy')).csv" -NoTypeInformation -Encoding UTF8
switch ($action){
Disable {$Computers_For_Action | Disable-ADAccount }
Delete {$Computers_For_Action | Remove-ADComputer -Confirm:$False }
}
}
Get-BadPC -Days 180 -Action Disable
- dannytveriaAug 28, 2021Brass Contributorfarismalaeb
thanks for your reply. I didn`t understand, your`s script is removing and disabling the Computer account in 180 days?- farismalaebAug 29, 2021Iron ContributorNo
You need only to change whats in line 20
set the number of date to whatever you want, and set the action you want to execute
so if you want to disable object which age 180 day
use the
Get-BadPC -Days 180 -Action Disable
to delete object older than 365 day use
Get-BadPC -Days 365 -Action Delete- dannytveriaAug 29, 2021Brass Contributorfarismalaeb
thanks for your help, but I goal is in 1 script to do both of the processes.