RJ96
RJ96

Reputation: 313

Filtering/spliting output in PowerShell

I am trying to filter/split the output I get from this code:

Get-ADUser -Filter * -Properties proxyaddresses |
    Where-Object {$_.proxyaddresses -Like "ixi*"} |
    Select sAMAccountName, proxyaddresses

Under "proxyaddresses" there's a number that starts with "ixi:+49" and some more information that I do not need. How can I manipulate this code to get only that number and then somehow find a way to filter it even more to get only the last four digits as my output?

Upvotes: 0

Views: 285

Answers (1)

Theo
Theo

Reputation: 61218

ProxyAddresses is an array, so -Like will not work on that. This might help if I understand the question correctly (untested)

$re = [regex]'ixi:\+49.*(\d{4})$'
Get-ADUser -Filter * -Properties ProxyAddresses |
    Where-Object {$_.ProxyAddresses -match $re.toString()} |
    Select-Object SamAccountName, ProxyAddresses,
                  @{Name = 'ixi'; Expression = {$re.Match($_.ProxyAddresses).Groups[1].Value}}

Regex details:

ixi:         Match the characters “ixi:” literally
\+           Match the character “+” literally
49           Match the characters “49” literally
.            Match any single character that is not a line break character
   *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(            Match the regular expression below and capture its match into backreference number 1
   \d        Match a single digit 0..9
      {4}    Exactly 4 times
)
$            Assert position at the end of the string (or before the line break at the end of the string, if any)

Upvotes: 1

Related Questions