Senior Systems Engineer
Senior Systems Engineer

Reputation: 1141

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

I am working on a 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:

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.

Upvotes: 0

Views: 99

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

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.

Upvotes: 1

Related Questions