Author Posts

April 16, 2015 at 8:11 pm

Hello,

I am using ADFIND to query Active Directory in my environment.
Using a Power Shell script to read all of the groups in my environment, I would like to work with groups that do not meet the requirements.

For example, if Group Names are not a specific length, I would like extract their owner.
Another query, would be to work with groups that may have spaces in their name. This should be done using regular expressions, but for some reason – it doesn't.

"^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.])[a-zA-Z]{2,6}$"/>

Appreciate any suggestions 🙂

April 16, 2015 at 10:20 pm

Hi Alex,

Please get familiar with the Active Directory cmdlets (https://technet.microsoft.com/en-us/library/ee617195.aspx) that come with the Remote Server Administration Tools. You really do not need to use ADFIND with PowerShell to get the results you want. Another alternative would be to use the .NET Framework classes in the System.DirectoryServices.ActiveDirectory namespace (https://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory%28v=vs.110%29.aspx).

You can find many many many examples on using the Active Directory cmdlets or .NET Framework classes using the major search engines (Google and Bing).

All the best to you,
Daniel

April 17, 2015 at 3:19 am

To my knowledge, you can't do any sort of regex filtering in the AD query itself. Even when you're using the -Filter parameter on the PowerShell AD cmdlets, those get translated into LDAP search syntax, which has no such functionality. You can search for all groups, and then do the regex filtering locally with any PowerShell code you want, but that will be slower.