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