Hello
I am writing a quick powershell script to disable inactive user and computer acounts, add a description and move them and seem to be stumbling while trying to make the filter more precise to fit my needs.
I am new in powershell use so bare with me. I was making a simple script with a few one-liners and stumbled upon Brian SW's post here about appending to the description field. Since that is exactly what i wanted I have basically modified some of the code he posted. Now i can make everything work however when i want to define more precise filter options i have problems applying the multiple filter conditions.
I simply would like to Filter with lastlogondate and strings in the description field. Now my problem here is my current code works fine but for some reason does not include computers that have nothing in the description field. So i think i need to add an -or (description -notlike "*") while still applying the lastlogondate condition. If i just add an -or obviously this will not work.
Import-Module ActiveDirectory
$then = (Get-Date).AddDays(-90)
$Computers = Get-ADComputer -Property Name,lastLogonDate -Filter {(lastLogonDate -lt $then) -and (description -notlike "*DO NOT DELETE*") -and (description notlike "*Disabled*")}
ForEach ($Computer in $Computers){
$ADComputer = Get-ADComputer $Computer -Properties Description
Set-ADComputer $ADComputer -Description "$($ADComputer.Description) - Computer Disabled on $(Get-Date)" -Enabled $false -PassThru | Move-ADObject -TargetPath "OU=Inactive Computers,DC=Altitude,DC=local"}
Thank you for your time!