Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell to get AD Object containing more than 3 not necessarily consecutive digits

People,

I need some help in the RegEx pattern for the below PowerShell code, so it can show any AD objects which has more than three digits as the name.

Ideally to match like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Test-DL1-1-25932404
Firstname LastName-1810023568
SERVER-1-2102285680

Script:

Get-ADObject -Filter * |
    Where-Object { $_.Name -match '\d\d\d\d' } |
    Format-Table -AutoSize

The script above returns all AD objects with numbers.

>Solution :

Get-ADObject -Filter * |
    Where-Object { $_.Name -match '\d{4,}' } |
    Format-Table -AutoSize

This will match 4 or more consecutive digits in the name (12345 but not 1-2-3-4-5).


Get-ADObject -Filter * |
    Where-Object { ($_.Name -replace '\D').Length -gt 3 } |
    Format-Table -AutoSize

This will get rid of all non-digits and count how many remain, and will get 1-2-3-4-5 as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading