0

I am trying to get the inactive users from AD with the following code:

# set the date (the number of days)
$NumberOfDays = 60

# set the timeframe ranging for the amount of days entered
$TimeRange = (Get-Date).Adddays(-($NumberOfDay))

# checks for inactive users within 60 days timeframe
Get-ADUser -Filter {LastLogonTimeStamp -lt $TimeRange } -Properties * 

and I would like to add this additional filter:

Get-ADUser -Filter {Name -like "f_*"}  -Properties * | Format-Table Name,SamAccountName

Can anyone help me how to merge these 2, I am a newbie and struggling :) .....

Elham Khan
  • 71
  • 8
  • See [this](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-7.3#long-description) link for info on using the -and/-or operator. Something like `Get-ADUser -Filter {(Name -like "f_*") -and (LastLogonTimeStamp -lt $TimeRange )}` – Vivek Kumar Singh Nov 15 '22 at 05:33

2 Answers2

1

Do note that -Filter expects a string rather than a script block.

Queries go a LOT faster with -LDAPFilter so use those if you can.

Ralph Sch
  • 77
  • 6
0

"See this link for info on using the -and/-or operator. Something like Get-ADUser -Filter {(Name -like "f_*") -and (LastLogonTimeStamp -lt $TimeRange )} – "

Vivek Kumar Singh from the comments

Elham Khan
  • 71
  • 8