# PowerShell Code not working

**URL:** <https://forums.powershell.org/t/powershell-code-not-working/19951>\
**Category:** PowerShell Help\
**Created:** [July 21, 2022, 2:25pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951 "2022-07-21T14:25:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MR1996](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@MR1996](https://forums.powershell.org/u/MR1996)\
**Post date:** [July 21, 2022, 2:25pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951/1 "2022-07-21T14:25:04Z")

</div>

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

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

```

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

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

```

will not give out hello as expected ?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 21, 2022, 2:58pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951/2 "2022-07-21T14:58:33Z")

</div>

Marco,  
Welcome to the forum. 👋🏽

There are actually a few issues …

```auto
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.

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

```

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

```auto
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`”.

> [@MR1996](#):
>
> will not give out hello as expected ?

What exactly do you actually want to achieve? 🤔

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

```auto
$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.

---

<div class="post-metadata">

**Author:** ![MR1996](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@MR1996](https://forums.powershell.org/u/MR1996)\
**Post date:** [July 21, 2022, 3:36pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951/3 "2022-07-21T15:36:41Z")

</div>

> [@Olaf](#):
>
> ```auto
> if (($User.ProxyAddresses -join ',') -match $Pattern) {
> 'Pattern exists'
> 
> ```

Thank you 🙂  
What is that exactly doing?

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 21, 2022, 3:50pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951/4 "2022-07-21T15:50:15Z")

</div>

> [@MR1996](#):
>
> What is that exactly doing?

Try it!

```auto
$User.ProxyAddresses -join ','

```

Operators in general:

> **[about Operators - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2)**
>
> Describes the operators that are supported by PowerShell.

Split and Join:

> **[about Operators - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2)**
>
> Describes the operators that are supported by PowerShell.

Comparison operators:

> **[about Comparison Operators - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.2)**
>
> Describes the operators that compare values in PowerShell.

Matching operators:

> **[about Comparison Operators - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.2)**
>
> Describes the operators that compare values in PowerShell.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/powershell-code-not-working/19951/5 "2024-05-16T20:17:40Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


