list all users who are carted from past 1 year. and check if they exists in a few DL's

poramboku used Ask the Experts™
This is not giving any results.
can you help find fix this..

I am trying to list all users who are carted from past 1 year. and check if they exists in a few DL's

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties * | ?{$_.Created -ge ((Get-Date).AddDays(-365)).Date -and ($_.MemberOf -contains "DL1" -or $_.MemberOf -contains "DL2")} | select -Property name, LastLogonDate, PasswordLastSet,UserPrincipalName,EmailAddress

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Try to debug one part at time.

Start with:

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties *

Do you get anything? if not, check you conditon

If you get something, then add the next condition

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties * | ?{$_.Created -ge ((Get-Date).AddDays(-365)).Date

and so on...
Lead SaaS Infrastructure Engineer
This should do the needful.

On my phone so can't test for syntax errors:


$DebugPreference = "Continue"

$AYearAgo = ((Get-Date).AddDays(-365)).Date

#Debug show the date a year ago
$AYearAgo | Write-Debug

$UsersAndGroups = Get-ADUser -Filter {whenCreated -ge $AYearAgo} -Properties whenCreated, MemberOf

#Debug show shortlist of users and groups
$UsersAndGroups | write-debug

#Output long list of users to groups:
Foreach ($User in $UsersAndGroups) {
Foreach ($Group in $($User| Select -ExpandProperty memberof) ) {
Write-Output "$User ---> $Group"



Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Ahh, I see you had a list of groups.  Okay we can modify to accomodate that and pull the matches only.  Almost finished with commute though, so I'll have to do that later, or somone else can expand upon this.
Douglas SuyemotoGlobal Support Services Engineer

Don't use
since you're trying a string match, instead use
-like "*DL1*"



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial