When i run a powershell script found on PowerShell Code: ADD an SMTP Address (Proxy Address) To Multiple Users From a .CSV File – Learn Azure, OpenAI, M365, Terraform, Cybersecurity i will get the following question for manual input:

cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:

My powershell script is:

Import-module ActiveDirectory
Import-Csv SMTPLIST.csv | ForEach-Object
{
$username = $_.samaccountname
$userproxy = $_.emailaddress -split ';'
Set-ADUser -Identity $username -Add @{proxyAddresses= $userproxy}
}

My csv is loaded in the directory where i run the powershell script. Somebody knows the solution?

5 Spice ups

What does the following produce?

Import-Csv SMTPLIST.csv

Also - if you do type something in, what happens.

ALso - what doe the CSV file look like.

CSV file is like :

j.doe;SMTP:j.doe@vermeulengroep.com
b.wijs;SMTP:b.wijs@vermeulengroep.com

When i type something , like enter, the screen disappears.

First try using a delmiiter with the importing of the csv, like this:

Import-Csv SMTPLIST.csv -Delimiter ';'

Also - what is the header in the CSV?

1 Spice up

I have tried with header:

samaccountname;emailaddress

But because i get the error i have tread it with and without.

I will get the following error:

PS C:\Users\beheer> C:\Users\beheer\powershell.ps1
Import-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "; " to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\beheer\powershell.ps1:2 char:36
+ Import-Csv SMTPLIST.csv -Delimiter '; ' | ForEach-Object
+                                    ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Import-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportCsvCommand
 

$username = $_.samaccountname
$userproxy = $_.emailaddress -split ';'
Set-ADUser -Identity $username -Add @{proxyAddresses= $userproxy}

This error appeared because of the space after the semi-colon ("; "). Try it without the space: “;”

1 Spice up

Try modifying the code.

Import-module ActiveDirectory
$Addresses = Import-Csv SMTPLIST.csv
ForEach($Address in $Addresses){
$username = $Address.samaccountname
$userproxy = $Address.emailaddress -split ';'
Set-ADUser -Identity $username -Add @{proxyAddresses= $userproxy}
}

Use Excel to import the CSV and change the column delimeter to “,” by saving the imported file as an actual CSV (Comma separated values) for “;” which is also used to separate the Proxy addresses in email address field. Also make sure your columns have a header field corresponding to the samaccountname and emailaddress properties of your CSV object. If you only have one email address per user then get rid of the -split “;”

Thank you for your answer. I have removed the split, because i have only one emailadress per line.

I will now get the following error per record:

Set-ADUser  Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At CUsersbeheerDocumentspowershell.ps16 char22
+ Set-ADUser -Identity $username -Add @{proxyAddresses= $userproxy}
+                      ~~~~~~~~~
    + CategoryInfo           InvalidData () [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId  ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

If i run the following code then i will get the error where i started with.

Import-module ActiveDirectory
Import-Csv SMTPLIST.csv -Delimiter ','| ForEach-Object
{
$username = $_.samaccountname
$userproxy = $_.emailaddress
Set-ADUser -Identity $username -Add @{proxyAddresses= $userproxy}
}

Error:

cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]: 

Try changing the code to

Import-module ActiveDirectory
Import-Csv SMTPLIST.csv -Delimiter ','| ForEach-Object
{
     $user = Get-ADUSer -Identity "$_.samaccountname" -Properties SamAccountName, DisplayName, ProxyAddresses
 
     $userproxy = $_.emailaddress

     $user.ProxyAddresses.add($userproxy)

     Set-ADUser -Instance $user
}

I pulled that from some code I have that I used in production, here is a snippit of that

$i = 0
		$addresses = @($exception.ProxyAddresses0,$exception.ProxyAddresses1,$exception.ProxyAddresses2,$exception.ProxyAddresses3,$exception.ProxyAddresses4,$exception.ProxyAddresses5,$exception.ProxyAddresses6)

		For ($i = 0; $i -lt 6; $i++) {

			$eaddress = $addresses[$i]
			If ($eaddress) {
				If (($eaddress.substring(0,4)) -clike "SMTP"){
					$Paddress = $eaddress.substring(5,(($eaddress.length)-5))
					Write-Host "Found Primary Email"
				}
				Write-Host "Assigning Proxy address"
				$user.ProxyAddresses.add($eaddress)
			}
			#write-host $eaddress
			#write-host $Paddress
			#read-host
		}
		Write-Host "Setting User Email Attribute"
		$user.mail = $Paddress
		break

Write-Host "Writing changes back to AD"
$result = Set-ADuSER -Instance $user

In my script I am getting all users and then using a for loop to process things against each user, but the principle should still apply to your one user

Also again make sure your csv is comma delimited, if it is you do not need to specify the delimeter. And ensure that the first row is a header row defining the first column as samaccountname and the second as email address.

I wonder if the error you are getting is because there are no headers defining the attributes you are referencing.