PowerShell Code not working

Hi, Im new here - I have got a little Problem with my code. It will not give out “hello”

$user=Get-ADUser -Filter * -Properties proxyaddresses | Where {$_.proxyaddresses -eq "SMTP:max.mustermann@max.com"} | select proxyaddresses
if ($user.proxyaddresses -contains "*max*") {
echo hallo

In the variable $user I get a list with all proxyaddresses. But the

if ($user.proxyaddresses -contains "*max*") {
echo hallo

will not give out hello as expected ?
Thanks a lot

Welcome to the forum. :wave:t4:

There are actually a few issues …

Get-ADUser -Filter * -Properties proxyaddresses

… not a n issue yet but you should consider using a -SearchBase the reduce the stress you put on your DC with those queries.

Where {$_.proxyaddresses -eq "SMTP:max.mustermann@max.com"}

… as the name of this property “proxyAddressES” already suggests it’s an multivalued property. So this comparison only works when there is only one proxyaddress.

if ($user.proxyaddresses -contains "*max*") {

The comparison operator -contains checks for the existence of an element in an array. And the element has to be an exact match.
And actually you’ve already checked for “*max*” when you checked for “SMTP:max.mustermann@max.com”.

What exactly do you actually want to achieve? :thinking:

ASsumed you’re looking for a particular user something like this could be a starter …

$SearchBase = 'OU=Users,OU=Office,OU=Berlin,OU=Germany,OU=Europe,DC=company,DC=de'
$SMTPAddress = 'SMTP:max.mustermann@max.com'
$Pattern = 'max'

$AllUserList = 
Get-ADUser -Filter * -SearchBase $SearchBase -Properties ProxyAddresses | 
    Where-Object {$_.ProxyAddresses -contains $SMTPAddress}

foreach ($User in $AllUserList) {
    if (($User.ProxyAddresses -join ',') -match $Pattern) {
        'Pattern exists'

But since a proxyaddress should be something unique the second step shouldn’t be necessary.

Regardless of all that - using aliasses in scripts or in forums is considered very bad style and it makes your code harder to read. Please avoid it.

Thanks in advance.

Thank you :slight_smile:
What is that exactly doing?

if (($User.ProxyAddresses -join ',') -match $Pattern) {
        'Pattern exists'

Try it!

$User.ProxyAddresses -join ','

Operators in general:

Split and Join:

Comparison operators:

Matching operators: