Filter out users that contains a word or character with Get-ADUser

大城市里の小女人 提交于 2020-01-06 15:03:45

问题


I am trying to get a list of AD-Users in different domains but cannot find a way to exclude users that has "Health" in their name for an example.

This is how my query looks like as of now:

#$excludedusers = @('HealthMailbox123456')
Get-ADUser -Server $test -Credential $1cred -Filter{enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' } | Select-object Samaccountname,surname,givenname | Where { $excludedusers -NotContains$_.Samaccountname }

As of now, "HealthMailbox123456" is excluded but only because I typed the entire name.

Is there a way to exclude every user having "Health" in their name?


回答1:


Get-ADUser -Server $test -Credential $1cred -Filter {Enabled -eq $true -and SamAccountName -notlike "*health*"}

Using the -filter switch instead of piping the result set into Where-Object reduces the amount of data which has to be send from the Domain Controller to the local system and is therefore the faster option.



来源:https://stackoverflow.com/questions/51760478/filter-out-users-that-contains-a-word-or-character-with-get-aduser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!