Add to the search of inactive users multiple OUs

tincho1980 25 Reputation points
2024-01-23T15:38:52.67+00:00

Hi all I came with a script that works fine what it does is to find users that haven't logged on in more than 90 days in an specific OU and then it disables them, however I need the script to search for 2 more OUs instead of only one but I couldn't make it work, this is the script. In this part Get-ADUser -Filter 'Enabled -eq $True' -SearchBase “OU TO SEARCH” i tried to create above something like this: $OU = 'OU1','OU2' and then tried to pipe it but it did not work.   Any thoughts how could I make it work? Many thanks!

#Script to disable users that not login for more than 90 days

#Create the report file
$FileName = "DisabledUsers" + (Get-Date).ToString("dd-MM-yyyy") + ".csv"
New-Item -Path "C:\temp" -Name $FileName -ItemType File
Add-Content -Path C:\temp\$fileName -Value "Account,Disabled date,Last Logon Date"
$DisabledDate = Get-Date -Format dd/MM/yyyy
$UsersToDisable = Get-ADUser -Filter 'Enabled -eq $True' -SearchBase “OU TO SEARCH” -Properties LastLogonDate,WhenCreated | where {$_.LastLogonDate -lt (get-date).AddDays(-90) -and $_.WhenCreated -lt (get-date).AddDays(-90)}
foreach($User in $UsersToDisable){
foreach($User in $UsersToDisable){
    if($User.DistinguishedName -notlike "OU TO NOT SEARCH"){
        Disable-ADAccount -Identity $User.SamAccountName -Confirm:$false 
        if((Get-ADUser -Identity $User.SamAccountName)){
            $Account = $User.SamAccountName
            $LastLogon = $User.LastLogonDate
            $Value = "$Account,$DisabledDate,$LastLogon"
            Add-Content -Path C:\temp\$FileName -Value $Value
            }
        }
    }
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Thameur-BOURBITA 36,261 Reputation points Moderator
    2024-01-23T16:29:41.03+00:00

    Hi @tincho1980

    If you want search in multiple OU You can try with $OU= @("OU1","OU2","OU3")

    I tried to adjust your script:

    #Script to disable users that not login for more than 90 days
    
    #Create the report file
    $FileName = "DisabledUsers" + (Get-Date).ToString("dd-MM-yyyy") + ".csv"
    New-Item -Path "C:\temp" -Name $FileName -ItemType File
    Add-Content -Path C:\temp\$fileName -Value "Account,Disabled date,Last Logon Date"
    $DisabledDate = Get-Date -Format dd/MM/yyyy
    $OU= @("OU1","OU2","OU3")
    foreach($OU in $OUs){
    $UsersToDisable = Get-ADUser -Filter 'Enabled -eq $True' -SearchBase “$OU” -Properties LastLogonDate,WhenCreated | where {$_.LastLogonDate -lt (get-date).AddDays(-90) -and $_.WhenCreated -lt (get-date).AddDays(-90)}
    
    foreach($User in $UsersToDisable){
    
    $DN= $User.DistinguishedName
    $Samaccountname = $User.SamAccountName
    $LastLogon = $User.LastLogonDate
        if($DN -notlike "OU TO NOT SEARCH"){
            Disable-ADAccount -Identity $SamAccountName -Confirm:$false 
            if((Get-ADUser -Identity $SamAccountName)){
                
                
                
                Add-Content -Path C:\temp\$FileName -Value "$SamAccountName,$DisabledDate,$LastLogon"
                }
            }
        }
        $DN= $null
        $Samaccountname = $null
        $LastLogon =$null
        }
    
    
    

    Please don't forget to accept helpful answer


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.