Adding a filter to a property

ivan rosa
ivan rosa used Ask the Experts™
on
Hi Folks,

i have wrote this line here to get a count of all users from an OU
(Get-ADUser -Filter "objectcategory -eq 'user'" -SearchBase "OU=microsoft=com").count

Open in new window


however i'm also trying to add a filter where it would only select active users... this line here i know will pick this specifici object but I don't know how to tell it just to pick the ones that are active
(Get-ADUser -Filter "objectcategory -eq 'user'" -SearchBase "ou=microsoft=com" -properties * | select-object enabled).count

Open in new window


does anybody have any idea on how to accompllish it?

thanks for looking
Comment
Watch Question

Do more with

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

Commented:
What do you mean by active?  Enabled?
(Get-ADUser -Filter "Enabled -eq '$true'" -SearchBase "OU=microsoft=com").count

Open in new window

The "objectcategory -eq 'user'" is redundant, since we're using the Get-ADUser cmdlet.
thank footech, the issue it was on the wording... i didn't know the existence of where-object

(Get-ADUser -Filter "objectcategory -eq 'user'" -SearchBase "OU=microsfot,=com" -properties * | Where-Object enabled -eq $true).count

Open in new window



i was trying to use it w/o the "objectcategory -eq 'user" but it would ask me for
cmdlet Get-ADUser at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Filter:
Top Expert 2014
Commented:
Yes, using Where-Object is definitely an option.  Filtering earlier is always better (more performant) so my example is preferred.  There are some limitations to what you can filter on using the -filter parameter however, so using Where-Object could be required in some scenarios.

To return all users with the Get-ADUser cmdlet, use -Filter *

Side note:  outside of testing, it's best not to use -Properties *, because that returns a lot of data.  Once you determine the attributes/properties that you need to work with (that are outside the default set), you can specify just those.

Author

Commented:
Makes sense, thanks bud!
Top Expert 2014

Commented:
Glad to help.

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