This is going to be a little different. As per usual, we need to follow our regular set of steps when dealing with a large amount of data that needs validation.
- Export list of users into CSV format
- Add new values into CSV
- Import CSV list with values
- Export list of users into CSV format
get-aduser -filter * -properties samaccountname | select samaccountname,mail | Export-Csv "C:\Users_add_proxy_addresses.csv"
2. Edit the CSV with the proxy email addresses you want. The format you need is the accountname (samaccountname), and proxyaddresses (SMTP:proxyemail@email.com). Like so below:
samaccountname | proxyaddresses |
rick.sanchez | SMTP:rick.sanchez@newproxyaddress.com |
rick.richardson | SMTP:rick.richardson@newproxyaddress.com |
Codie.Youthead | SMTP:Codie.Youthead@newproxyaddress.com |
3. Import the .CSV file with some code:
Import-module ActiveDirectory
$Imported_csv = Import-Csv -Path "C:\Users_add_proxy_addresses.csv"
foreach ($user in $Imported_csv)
{
$User.samaccountname
$User.proxyaddresses
Set-ADUser -Identity $User.samaccountname -Add @{proxyAddresses= $User.proxyaddresses}
}
$total = ($Imported_csv).count
$total
write-host "AD accounts added with proxy addresses..."
Or , if you want to add a certain SMTP extension use this code from a SAMaccountname CSV file for all users:
$Imported_csv = Import-Csv -Path "C:\Users_add_proxy_addresses.csv"
foreach ($user in $Imported_csv)
{
$User.samaccountname
Set-ADUser -Identity $User.samaccountname -Add @{proxyAddresses= "SMTP:" + $User.samaccountname + "@newproxyaddress.com"}
}
$total = ($Imported_csv).count
$total
write-host "AD accounts added with proxy addresses..."
Make sure to check your work:
Get-ADUser -Filter * -Properties SamAccountname, proxyAddresses | where proxyAddresses -ne $null | select-object samaccountname,proxyaddresses | out-gridview
The above only shows ONE proxy address at a time. Since the attribute proxy-address can actually store more than one value, it’s an array.
Showing the results
The Get-ADUser cmdlet does the job nicely. Although, it’s not quite as neat as I would like:
get-aduser -filter * -properties samaccountname, proxyaddresses | Select-object samAccountName, proxyaddresses | Out-GridView
A sample output below shows the results of the proxyaddress attributes. Notice how all the different proxies are put together in the same column.
![](http://fatdex.ca/wp-content/uploads/2020/09/2020-09-30-P-Proxyaddress-001.png)
This is OK, and does require some finer tweaking with a CSV editor. However, there’s got to be a way to display each proxy address independently in their own column.
I did a little searching and I found this from the devblogs microsoft guys:
Get-ADUser -Filter * -Properties proxyaddresses | select samaccountname, @{L='ProxyAddress_1'; E={$_.proxyaddresses[0]}}, @{L=’ProxyAddress_2';E={$_.ProxyAddresses[1]}} | out-gridview
This lists out the proxy addresses by column with some help from the select statement above.
![](https://fatdex.ca/wp-content/uploads/2020/09/2020-09-30-P-Proxyaddress-002.png)
One thought on “PowerShell – Adding Proxy Addresses by CSV”
RM
Thanks it works for me!