How to filter a variable with multiple operators?

T Crha 381 Reputation points

Hello everyone,
I need to store proxy addresses into a variable, but with an exception of certain specific address. How do I pass a filter to proxyaddresses attribute?
For example, if I want to select only address that start with "SMTP:" and ends with "", I do it like this:

I define a user that I want to work with :
$users = get-aduser -filter "samaccountname -eq 'somebody'

I select proxyaddresses from his AD user profile:
$userProxy = get-aduser $user -Properties * | select proxyaddresses

And by using variable property StartsWith and EndsWith I am able to define what I need (and store it into variable):
$userProxyFiltered = $userProxy.Proxyaddresses.Where( { $.StartsWith( 'SMTP:' ) -AND $.EndsWith('@keyman .com')} )

But I also need an opposite thing to that - I need to store into a second variable all proxy addresses that do not end with, and do not start with X500

What are the other methods that I can use to filter a variable content apart from StartsWith and EndsWith? or what other way would you filter a specific content from a variable?

If I do it like this
$someVariable = ({$userProxy.proxyaddresses -notlike 'SIP:'} -AND {$userProxy.proxyaddresses -notlike 'X500:'})
All I get is
PS C:\WINDOWS\system32>> $someVariable

EDIT: Or, If I take a look at the Contains method here
Is there a way to negate the method, to use something like .NotContains?

I hope I explained myself enough, thanks for any hints or comments


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,275 questions
0 comments No comments
{count} votes

4 additional answers

Sort by: Most helpful
  1. Newbie Jones 1,296 Reputation points

    First of all, you don't need to use the filter in Get-ADUser when using SamAccountName as this is an acceptable value for the identity parameter.

    So you just need to use

    $user = Get-ADUser SamAccountName -properties Proxyaddresses

    Please don't use the * to retrieve all attributes which you then mostly drop. It affects the performance of scripts. Just bring back the properties you need.

    To get your filter working correctly, you need to pipe the results into a Where-Object like you have in your first example. The way you have it will only return true or false.

    $userProxyFiltered = $user.Proxyaddresses | Where xxxxx

    0 comments No comments

  2. T Crha 381 Reputation points

    thank you for your answer. However, I dont understand how should I do what you are suggesting - if I go like this:
    $userProxyFiltered = $userProxy.Proxyaddresses | where {$_.userProxy.Proxyaddresses -notmatch '@keyman .com'}
    I still end up with all the proxyaddresses user has on his AD account

    Can you elaborate a bit more please?

    Thank you

    0 comments No comments

  3. Newbie Jones 1,296 Reputation points

    Your also missing the wildcards.

    The following should work.

    (Get-ADUser SamAccountName -properties proxyaddresses).proxyaddresses | Where {$_ -NotLike 'sip:*' -and $_ -NotLike 'X500*'}  
    0 comments No comments

  4. T Crha 381 Reputation points

    Thats it, now I am able to move on with the script
    Thank you very much!

    0 comments No comments