如何查找DisplayName属性中非空白字符及括号前无空格的AD用户?
AD User Filtering Solutions for DisplayName Requirements
Hey there! Let's tackle your two AD user filtering needs with straightforward PowerShell commands and clear explanations:
1. Filter Users with Non-Blank DisplayName
To find AD users where the DisplayName property has actual non-whitespace content (excluding empty values or entries with only spaces), use this command:
Get-ADUser -Filter * -Properties DisplayName | Where-Object { $_.DisplayName -notmatch '^\s*$' }
- The regex
^\s*$matches strings that are either empty or made up entirely of whitespace characters. - Using
-notmatchreverses this logic, so we only retain users with aDisplayNamethat contains meaningful text.
2. Filter Users with DisplayName Format Like John Wick(IT Engineer) (No Space Before Parentheses)
You already have the command to find users with two or more spaces before parentheses (\s{2,}\(). To target the opposite scenario—where there's no space at all before the opening parenthesis—we'll use a negative lookbehind assertion in regex:
Get-ADUser -Filter * -Properties DisplayName | Where-Object { $_.DisplayName -match '(?<!\s)\(' }
- The regex
(?<!\s)\(translates to: "match an opening parenthesis(that is NOT preceded by a whitespace character". - This will accurately catch entries like
John Wick(IT Engineer)where the parenthesis directly follows the name without any space, while excluding users with spaces before the(.
内容的提问来源于stack exchange,提问作者Arbelac
相关产品推荐
相关产品推荐

