I am looking for some help with my one liner, or a better approach. I am attempting to do user cleanup, but I have a very large dataset, 2 million users. The code below does work for me, and generally returns around 700K accounts. The problem I am having is the filter is inconsistent and I need to recurse the group I am trying to exclude. I am trying to stay away from storing the data to an array because of the large size, where-object seems to be a bad idea too. Any help from the PS community would be most appreciated.
I have tried recursing the group with get-adgroupmember and outputting to an array, but the filter in get-aduser didn’t work.
Consider using the -LDAPFilter parameter (much faster than -Filter). Because we want to filter as far left as possible, we need to convert the date into ticks so we can use it in our LDAP query. I think the below example will help you get going in the right direction.
The problem is memberof is an array. “memberof -ne ‘DN of AD group’” will always be true (unless memberof contains only one group), since it returns the other groups that don’t match. I’ve been unsuccessful with -notlike. -notlike treats arrays in the same way. Unfortunately -contains isn’t supported. Using -not and -eq seems to be the only way to get at nonmembers. (find users NOT in group) The filter is actually not a script block, even though script blocks get converted to strings and work. (And the official documentation in error has a script block filter example.) Script blocks won’t work in all cases.
The parentheses may be optional here, but usually you would need them with -not and -eq together.