SOLVED

Disable and removal of Computer accounts

%3CLINGO-SUB%20id%3D%22lingo-sub-2687519%22%20slang%3D%22en-US%22%3EDisable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2687519%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20made%20a%20script%20to%20disable%20old%20computer%20accounts.%3C%2FP%3E%3CP%3EMy%20Sysadmin%20asked%20me%20to%20disable%20after%20180%20days%20and%20remove%20them%20after%20a%20year.%3C%2FP%3E%3CP%3EMy%20goal%20is%20to%20disable%20computer%20accounts%20after%20180%20days%20and%20export%20to%20CSV%3C%2FP%3E%3CP%3Ethen%20delete%20computer%20accounts%20after%20356%20days%20and%20export%20to%20CSV.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImport-Module%20ActiveDirectory%3C%2FP%3E%3CP%3E%23%20Set%20the%20Parameters%20since%20last%20logon%3CBR%20%2F%3E%24DaysInactive%20%3D%20180%3CBR%20%2F%3E%24InactiveDate%20%3D%20(Get-Date).Adddays(-(%24DaysInactive))%3CBR%20%2F%3E%24ForDisableLog%20%3D%20%22C%3A%5Cscripts%5CComputerAccounts%5CLogs%5CFor_Disable%24((Get-Date).ToString('dd-MM-yyyy')).csv%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23-------------------------------%3CBR%20%2F%3E%23%20FIND%20INACTIVE%20COMPUTERS%3CBR%20%2F%3E%23-------------------------------%3C%2FP%3E%3CP%3E%23%20Automated%20way%20(includes%20never%20logged%20on%20computers)%3CBR%20%2F%3E%24Computers_For_Disable%20%3D%20Search-ADAccount%20-AccountInactive%20-DateTime%20%24InactiveDate%20-ComputersOnly%20-SearchBase%20%22DC%3Dstaff%20%2CDC%3Dlocal%22%20%7C%20Where-Object%20%7B%24_.distinguishedname%20-notlike%20%22*%2COU%3DServers%2C*%22%7D%20%7C%20Where-Object%20%7B%24_.distinguishedname%20-notlike%20%22*%2COU%3DTest%2C*%22%7D%20%7C%20Where-Object%20%7B%24_.distinguishedname%20-notlike%20%22*%2COU%3DIT%2C*%22%7D%20%7C%20Where-Object%20%7B%24_.distinguishedname%20-notlike%20%22*%2COU%3DLaptops%2C*%22%7D%20%7C%20Where-Object%20%7B%24_.distinguishedname%20-notlike%20%22*%2CCN%3DComputers%2C*%22%7D%20%7C%20Select-Object%20Name%2C%20LastLogonDate%2C%20Enabled%2C%20DistinguishedName%3C%2FP%3E%3CP%3E%23-------------------------------%3CBR%20%2F%3E%23%20REPORTING%3CBR%20%2F%3E%23-------------------------------%3CBR%20%2F%3E%23%20Export%20results%20to%20CSV%3C%2FP%3E%3CP%3E%24Computers_For_Disable%20%7C%20Export-Csv%20%22C%3A%5Cscripts%5CComputerAccounts%5CLogs%5CFor_Disable%24((Get-Date).ToString('dd-MM-yyyy')).csv%22%20-NoTypeInformation%20-Encoding%20UTF8%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2687519%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2692692%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2692692%22%20slang%3D%22en-US%22%3EDo%20you%20have%20any%20question%20or%20you%20are%20just%20sharing%20the%20script%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2693216%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2693216%22%20slang%3D%22en-US%22%3EYes%2C%3CBR%20%2F%3EI%20trying%20to%20disable%20the%20computer%20accounts%20that%20have%20not%20been%20logged%20for%20180%20days%2C%20and%20export%20them%20on%20CSV%20file.%3CBR%20%2F%3EAlso%2C%20I%20want%20to%20delete%20the%20computer's%20accounts%20that%20have%20not%20been%20logged%20for%20365%20days%2C%20and%20export%20them%20as%20well.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2694894%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2694894%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wrote%20a%20quick%20script%20to%20do%20what%20you%20need%2C%20but%20please%20note%3C%2FP%3E%3CP%3Ethis%20script%20will%20REMOVE%2C%20and%20DISABLE%20adcomputer%20account%2C%20test%20it%20first%20and%20make%20sure%20its%20doing%20the%20result%20you%20need%20before%20applying%20it%20to%20production..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20what%20you%20need%20to%20change%20only%20is%20the%20last%20line%2C%26nbsp%3B%3C%2FP%3E%3CP%3EGet-BadPC%20-Days%20180%20-Action%20Disable%3C%2FP%3E%3CP%3EDays%3D%20what%20ever%20number%20of%20day%3C%2FP%3E%3CP%3Eand%20action%20if%20you%20want%20to%20delete%20or%20disable.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Get-BadPC%7B%0Aparam(%0A%5Bparameter(mandatory%3D%24true)%5D%24Days%2C%0A%5Bparameter(mandatory%3D%24true)%5D%0A%5BValidateSet('Delete'%2C'Disable')%5D%24Action%0A)%0A%24InactiveDate%20%3D%20(Get-Date).Adddays(-(%24Days))%0A%24Computers_For_Action%20%3D%20Search-ADAccount%20-AccountInactive%20-DateTime%20%24InactiveDate%20-ComputersOnly%20-SearchBase%20%22DC%3DTest%20%2CDC%3Dlocal%22%20%7C%20Where-Object%20%7B(%24_.distinguishedname%20-notlike%20%22*%2COU%3DServers%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DTest%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DIT%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DLaptops%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2CCN%3DComputers%2C*%22)%7D%20%0A%24Computers_For_Action%20%7C%20Export-Csv%20%22C%3A%5CUsers%5CAdministrator%5CDownloads%5C%24(%24Action)-%24((Get-Date).ToString('dd-MM-yyyy')).csv%22%20-NoTypeInformation%20-Encoding%20UTF8%0A%0Aswitch%20(%24action)%7B%0ADisable%20%7B%24Computers_For_Action%20%7C%20Disable-ADAccount%20%7D%0ADelete%20%7B%24Computers_For_Action%20%7C%20Remove-ADComputer%20-Confirm%3A%24False%20%7D%0A%0A%7D%0A%0A%7D%0A%0A%0AGet-BadPC%20-Days%20180%20-Action%20Disable%20%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2694986%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2694986%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3CBR%20%2F%3Ethanks%20for%20your%20reply.%20I%20didn%60t%20understand%2C%20your%60s%20script%20is%20removing%20and%20disabling%20the%20Computer%20account%20in%20180%20days%3F%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2696001%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2696001%22%20slang%3D%22en-US%22%3ENo%3CBR%20%2F%3EYou%20need%20only%20to%20change%20whats%20in%20line%2020%3CBR%20%2F%3Eset%20the%20number%20of%20date%20to%20whatever%20you%20want%2C%20and%20set%20the%20action%20you%20want%20to%20execute%3CBR%20%2F%3Eso%20if%20you%20want%20to%20disable%20object%20which%20age%20180%20day%3CBR%20%2F%3Euse%20the%3CBR%20%2F%3EGet-BadPC%20-Days%20180%20-Action%20Disable%3CBR%20%2F%3Eto%20delete%20object%20older%20than%20365%20day%20use%3CBR%20%2F%3EGet-BadPC%20-Days%20365%20-Action%20Delete%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2696059%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2696059%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3CBR%20%2F%3Ethanks%20for%20your%20help%2C%20but%20I%20goal%20is%20in%201%20script%20to%20do%20both%20of%20the%20processes.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2696646%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2696646%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20use%20the%20script%20provided%20by%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%2C%20you%20only%20need%20to%20add%26nbsp%3B%3CEM%3EGet-BadPC%20-Days%20365%20-Action%20Delete%26nbsp%3B%3C%2FEM%3Eon%20row%2021%20and%20save%20the%20file%20as%20a%20ps1%20file.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Get-BadPC%7B%0Aparam(%0A%5Bparameter(mandatory%3D%24true)%5D%24Days%2C%0A%5Bparameter(mandatory%3D%24true)%5D%0A%5BValidateSet('Delete'%2C'Disable')%5D%24Action%0A)%0A%24InactiveDate%20%3D%20(Get-Date).Adddays(-(%24Days))%0A%24Computers_For_Action%20%3D%20Search-ADAccount%20-AccountInactive%20-DateTime%20%24InactiveDate%20-ComputersOnly%20-SearchBase%20%22DC%3DTest%20%2CDC%3Dlocal%22%20%7C%20Where-Object%20%7B(%24_.distinguishedname%20-notlike%20%22*%2COU%3DServers%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DTest%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DIT%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DLaptops%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2CCN%3DComputers%2C*%22)%7D%20%0A%24Computers_For_Action%20%7C%20Export-Csv%20%22C%3A%5CUsers%5CAdministrator%5CDownloads%5C%24(%24Action)-%24((Get-Date).ToString('dd-MM-yyyy')).csv%22%20-NoTypeInformation%20-Encoding%20UTF8%0A%0Aswitch%20(%24action)%7B%0ADisable%20%7B%24Computers_For_Action%20%7C%20Disable-ADAccount%20%7D%0ADelete%20%7B%24Computers_For_Action%20%7C%20Remove-ADComputer%20-Confirm%3A%24False%20%7D%0A%0A%7D%0A%0A%7D%0A%0A%0AGet-BadPC%20-Days%20180%20-Action%20Disable%0AGet-BadPC%20-Days%20365%20-Action%20Delete%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThen%20you%20can%20run%20the%20script%20from%20PowerShell%20by%20just%20running%20.%5CScriptLocation%5Cscriptname.ps1%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20also%20add%20some%20logging%20to%20the%20script%20with%20the%20Write-Log%20Function%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Write-Log(%5Bstring%5B%5D%5D%24Message%2C%20%5Bstring%5D%24LogFile%20%3D%20%24Script%3ALogFile%2C%20%5Bswitch%5D%24ConsoleOutput%2C%20%5BValidateSet(%22SUCCESS%22%2C%20%22INFO%22%2C%20%22WARN%22%2C%20%22ERROR%22%2C%20%22DEBUG%22)%5D%5Bstring%5D%24LogLevel)%0A%7B%0A%20%24Message%20%3D%20%24Message%20%2B%20%24Input%0A%20If%20(!%24LogLevel)%20%7B%20%24LogLevel%20%3D%20%22INFO%22%20%7D%0A%20switch%20(%24LogLevel)%0A%20%7B%0A%20%20SUCCESS%20%7B%20%24Color%20%3D%20%22Green%22%20%7D%0A%20%20INFO%20%7B%20%24Color%20%3D%20%22White%22%20%7D%0A%20%20WARN%20%7B%20%24Color%20%3D%20%22Yellow%22%20%7D%0A%20%20ERROR%20%7B%20%24Color%20%3D%20%22Red%22%20%7D%0A%20%20DEBUG%20%7B%20%24Color%20%3D%20%22Gray%22%20%7D%0A%20%7D%0A%20if%20(%24Message%20-ne%20%24null%20-and%20%24Message.Length%20-gt%200)%0A%20%7B%0A%20%20%24TimeStamp%20%3D%20%5BSystem.DateTime%5D%3A%3ANow.ToString(%22yyyy-MM-dd%20HH%3Amm%3Ass%22)%0A%20%20if%20(%24LogFile%20-ne%20%24null%20-and%20%24LogFile%20-ne%20%5BSystem.String%5D%3A%3AEmpty)%0A%20%20%7B%0A%20%20%20Out-File%20-Append%20-FilePath%20%24LogFile%20-InputObject%20%22%5B%24TimeStamp%5D%20%24Message%22%0A%20%20%7D%0A%20%20if%20(%24ConsoleOutput%20-eq%20%24true)%0A%20%20%7B%0A%20%20%20Write-Host%20%22%5B%24TimeStamp%5D%20%5B%24LogLevel%5D%20%3A%3A%20%24Message%22%20-ForegroundColor%20%24Color%0A%20%20%7D%0A%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EYou%20will%20have%20some%20reference%20for%20later%20on%20which%20devices%20have%20been%20disabled%20and%20which%20are%20deleted.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2696941%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2696941%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20point%20of%20creating%20a%20function%20is%20to%20reduce%20code%20duplication%2C%20plus%20it%20provides%20better%20control%20of%20the%20business%20requirement.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20only%20change%20you%20need%20to%20do%20is%20add%201%20extra%20line%20as%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1071311%22%20target%3D%22_blank%22%3E%40R_Gijsbers_Rademakers%3C%2FA%3E%26nbsp%3Bsay%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGet-BadPC%20-Days%20180%20-Action%20Disable%0AGet-BadPC%20-Days%20365%20-Action%20Delete%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2712677%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2712677%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3Ebig%20thanks%20for%20you%20help%20guys.%3CBR%20%2F%3Ebut%20I%20tried%20on%20a%20test%20envoirment%2C%20and%20the%20script%20remove%20computer%20accounts%20from%20ou%20that%20I%20not%20wanted%2C%20like%20in%20line%2021%3CBR%20%2F%3E%7C%20Where-Object%20%7B(%24_.distinguishedname%20-notlike%20%22*%2COU%3DServers%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DTest%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DIT%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DLaptops%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2CCN%3DComputers%2C*%22)%7D%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2712777%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2712777%22%20slang%3D%22en-US%22%3EI%20found%20the%20problem%20the.%3CBR%20%2F%3EI%20saw%20that%20at%20the%20CSV%20file%20showing%20the%20computer%20accounts%20that%20in%20the%20OU%20that%20I%20didn't%20want%20to%20delete%20or%20disable.%3CBR%20%2F%3EThere%20is%20a%20way%20that%20this%20computer%20account%20will%20not%20show%20in%20the%20disable%20or%20remove%20logs%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2716898%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2716898%22%20slang%3D%22en-US%22%3ESimply%20add%20the%20OU%20to%20the%20exception%20list%2C%20so%20you%20can%20add%20extra%20-or%20condition%3CBR%20%2F%3E-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DAnotherOUtoExclude%2C*%22)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2717762%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2717762%22%20slang%3D%22en-US%22%3EI%20didn't%20understand%20how%20to%20do%20it%3CBR%20%2F%3EI%20don't%20want%20to%20see%20the%20ou%20that%20I%20exclude%20at%20the%20csv%20file%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2719894%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2719894%22%20slang%3D%22en-US%22%3EThe%20Question%20is%20did%20the%20OU%20match%20any%20of%20the%20filter%3CBR%20%2F%3E%24Computers_For_Action%20%3D%20Search-ADAccount%20-AccountInactive%20-DateTime%20%24InactiveDate%20-ComputersOnly%20-SearchBase%20%22DC%3DTest%20%2CDC%3Dlocal%22%20%7C%20Where-Object%20%7B(%24_.distinguishedname%20-notlike%20%22*%2COU%3DServers%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DTest%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DIT%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2COU%3DLaptops%2C*%22)%20-or%20(%24_.distinguishedname%20-notlike%20%22*%2CCN%3DComputers%2C*%22)%7D%3CBR%20%2F%3E%3CBR%20%2F%3ETry%20to%20run%20this%20line%20alone%20and%20then%20call%20the%20%24Computers_For_Action%20variable.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2720797%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2720797%22%20slang%3D%22en-US%22%3EI%20want%20script%20will%20check%20in%20all%20the%20domain%20but%20not%20in%20ou's%20that%20I%20exclude%20up.%3CBR%20%2F%3EAlso%20I%20don't%20want%20to%20see%20the%20computer%20accounts%20in%20that%20ou%20on%20the%20csv%20file%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2721241%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%20and%20removal%20of%20Computer%20accounts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2721241%22%20slang%3D%22en-US%22%3EYes%2C%20the%20ou%20matches%20the%20filter%20-notlike.%3CBR%20%2F%3Ebut%20I%20don't%20want%20to%20see%20their%20computer%20accounts%3C%2FLINGO-BODY%3E
Contributor

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 to CSV

then delete computer accounts after 356 days and export to CSV.

 

Import-Module ActiveDirectory

# Set the Parameters since last logon
$DaysInactive = 180
$InactiveDate = (Get-Date).Adddays(-($DaysInactive))
$ForDisableLog = "C:\scripts\ComputerAccounts\Logs\For_Disable$((Get-Date).ToString('dd-MM-yyyy')).csv"

 

#-------------------------------
# FIND INACTIVE COMPUTERS
#-------------------------------

# Automated way (includes never logged on computers)
$Computers_For_Disable = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=staff ,DC=local" | Where-Object {$_.distinguishedname -notlike "*,OU=Servers,*"} | Where-Object {$_.distinguishedname -notlike "*,OU=Test,*"} | Where-Object {$_.distinguishedname -notlike "*,OU=IT,*"} | Where-Object {$_.distinguishedname -notlike "*,OU=Laptops,*"} | Where-Object {$_.distinguishedname -notlike "*,CN=Computers,*"} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName

#-------------------------------
# REPORTING
#-------------------------------
# Export results to CSV

$Computers_For_Disable | Export-Csv "C:\scripts\ComputerAccounts\Logs\For_Disable$((Get-Date).ToString('dd-MM-yyyy')).csv" -NoTypeInformation -Encoding UTF8

29 Replies
Do you have any question or you are just sharing the script?
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.

@dannytveria 

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 
@farismalaeb
thanks for your reply. I didn`t understand, your`s script is removing and disabling the Computer account in 180 days?
No
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
@farismalaeb
thanks for your help, but I goal is in 1 script to do both of the processes.

@dannytveria 

If you use the script provided by @farismalaeb, you only need to add Get-BadPC -Days 365 -Action Delete on row 21 and save the file as a ps1 file.

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
Get-BadPC -Days 365 -Action Delete

Then you can run the script from PowerShell by just running .\ScriptLocation\scriptname.ps1

 

You could also add some logging to the script with the Write-Log Function 

function Write-Log([string[]]$Message, [string]$LogFile = $Script:LogFile, [switch]$ConsoleOutput, [ValidateSet("SUCCESS", "INFO", "WARN", "ERROR", "DEBUG")][string]$LogLevel)
{
	$Message = $Message + $Input
	If (!$LogLevel) { $LogLevel = "INFO" }
	switch ($LogLevel)
	{
		SUCCESS { $Color = "Green" }
		INFO { $Color = "White" }
		WARN { $Color = "Yellow" }
		ERROR { $Color = "Red" }
		DEBUG { $Color = "Gray" }
	}
	if ($Message -ne $null -and $Message.Length -gt 0)
	{
		$TimeStamp = [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss")
		if ($LogFile -ne $null -and $LogFile -ne [System.String]::Empty)
		{
			Out-File -Append -FilePath $LogFile -InputObject "[$TimeStamp] $Message"
		}
		if ($ConsoleOutput -eq $true)
		{
			Write-Host "[$TimeStamp] [$LogLevel] :: $Message" -ForegroundColor $Color
		}
	}
}

You will have some reference for later on which devices have been disabled and which are deleted. 

@dannytveria 

The point of creating a function is to reduce code duplication, plus it provides better control of the business requirement.

 

the only change you need to do is add 1 extra line as @R_Gijsbers_Rademakers say

Get-BadPC -Days 180 -Action Disable
Get-BadPC -Days 365 -Action Delete

 

Thanks

 

Hi,
big thanks for you help guys.
but I tried on a test envoirment, and the script remove computer accounts from ou that I not wanted, like in line 21
| 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,*")}
I found the problem the.
I saw that at the CSV file showing the computer accounts that in the OU that I didn't want to delete or disable.
There is a way that this computer account will not show in the disable or remove logs?
Simply add the OU to the exception list, so you can add extra -or condition
-or ($_.distinguishedname -notlike "*,OU=AnotherOUtoExclude,*")
I didn't understand how to do it
I don't want to see the ou that I exclude at the csv file
The Question is did the OU match any of the filter
$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,*")}

Try to run this line alone and then call the $Computers_For_Action variable.

I want script will check in all the domain but not in ou's that I exclude up.
Also I don't want to see the computer accounts in that ou on the csv file
Yes, the ou matches the filter -notlike.
but I don't want to see their computer accounts
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,*")} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName 
$Computers_For_Action | Export-Csv "C:\Users\Administrator\Desktop\$($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 
Get-BadPC -Days 356 -Action Delete

This is my script and i get the next error

Disable-ADAccount : The input object cannot be bound to any parameters for the command either because the command does not take pipel
ine input or the input and its properties do not match any of the parameters that take pipeline input.

@dannytveria Hi

Yes, this is expected if the number of computer object returned is zero.

You can see the result by running the query it self

$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,*")} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName 

Write-Host "The Number of hosts in your search is " -NoNewline
Write-Host "$($Computers_For_Action.count)" -ForegroundColor Green
I didn`t understand why the script not working.
as I posted earlier
What is the output of the last script, are you able to get the result ?