# How to pull Primary SMPT from secondary SMTP

**URL:** <https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412>\
**Category:** PowerShell Help\
**Created:** [October 4, 2018, 9:10am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412 "2018-10-04T09:10:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maelito](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@maelito](https://forums.powershell.org/u/maelito)\
**Post date:** [October 4, 2018, 9:10am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/1 "2018-10-04T09:10:08Z")

</div>

Hi

I have a list of user’s secondary SMTP and I am trying to find the Primary one based on that list.

This is my script which doesn’t work as expected.

```
get-aduser -Properties \* | Where-Object $.\_proxyaddresses -Like (Get-Content C:\Temp\SMTP\list.txt) | ForEach-Object {

? {$\_ -match ‘^smtp’} {

[pscustomobject]@{

name = $\_.name

SMTPproxyaddresses = $addr

}

}

}
```
 

Any pointers would be highly appreciated.

Cheers, M

&nbsp;

---

<div class="post-metadata">

**Author:** ![pdearmen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pdearmen/32/263_2.png) [@pdearmen](https://forums.powershell.org/u/pdearmen)\
**Post date:** [October 4, 2018, 11:42am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/2 "2018-10-04T11:42:15Z")

</div>

There is a lot going on there - but I checked and I have something from earlier this year I wrote for work with our office 365 migration - and here is a modified version below.

for efficiency you only want to get the users which are in your file - i used csv - but you can change the import-csv to get-content (and then change the get-aduser line)

```
foreach ($Item in Import-Csv c:\temp\users.csv)
{
	$user = Get-ADUser $($Item.user) -Properties proxyaddresses
	$primaySMTP = ( $user | Select-Object -ExpandProperty | Where-Object{ $_ -clike "SMTP:*" }).replace("SMTP:", "")
	[pscustomobject]@{
		"Name" = "$($user.name)"
		"SMTPProxyAddressses" = ($user.proxyaddresses) -join ";"
		"PrimarySMTP" = $primaySMTP
	}
}
```

---

<div class="post-metadata">

**Author:** ![maelito](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@maelito](https://forums.powershell.org/u/maelito)\
**Post date:** [October 5, 2018, 3:10am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/3 "2018-10-05T03:10:50Z")

</div>

Hi

The csv will contain only the users smtp address so proxyaddresses but smtp from which i want to pull the SMTP address.

Your script iterates on user which is not on the csv.

M

---

<div class="post-metadata">

**Author:** ![maelito](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@maelito](https://forums.powershell.org/u/maelito)\
**Post date:** [October 5, 2018, 3:10am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/4 "2018-10-05T03:10:50Z")

</div>

Hi

The csv will contain only the users smtp address so proxyaddresses but smtp from which i want to pull the SMTP address.

Your script iterates on user which is not on the csv.

M

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 5, 2018, 4:08am UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/5 "2018-10-05T04:08:21Z")

</div>

Your script seems to be broken or incomplete.  
From what I understood, you are trying to get all the users whose proxy addresses are in your list.

```
$SMTP = Get-Content -Path C:\Temp\SMTP\list.txt
Get-ADUser -Filter * -Properties * | Where-Object {$._proxyaddresses -in $SMTP} | ForEach-Object -Process {
#Do what you want
}
```

Note: Get-AdUser has identity as a default mandatory parameter, if you don’t have a specific identity but need all , you have to use -filter with \*(wildcard) as value.

And you have to put your script between the **pre** tags for formatting when posting in the forum.

---

<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:34pm UTC](https://forums.powershell.org/t/how-to-pull-primary-smpt-from-secondary-smtp/11412/6 "2024-05-16T20:34:28Z")

</div>

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


