I’m a PowerShell noob, but I’ve used Exchange for over 2 decades. I’m trying to follow some instructions a coworker has left me to migrate mailboxes to the cloud, and the step I’m on requires me to build an SMTP list from the SAM account name.

Import-CSV C:\temp\users-output.csv | ForEach {Get-Recipient -Identity $_.Name | Select PrimarySmtpAddress} -ResultSize Unlimited | Export-CSV C:\temp\resume.csv -Encoding UTF8 -NTI

When I attempt to run this in PowerShell, I get the following error:

ForEach-Object : Cannot bind parameter ‘RemainingScripts’. Cannot convert the “-ResultSize” value of type
“System.String” to type “System.Management.Automation.ScriptBlock”.
At line:1 char:39

  • … utput.csv | ForEach {Get-Recipient -Identity $_.Name | Select Primary …
  • CategoryInfo : InvalidArgument: (:slight_smile: [ForEach-Object], ParameterBindingException
  • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

Do you know what I’m doing wrong? Do you have any suggestions on how to create a list of SMTP addresses from a list of SAM account names?

5 Spice ups

It looks like the -ResultSize parameter got moved outside of the loop. Try this:

Import-CSV C:\temp\users-output.csv | 
    ForEach {Get-Recipient -Identity $_.Name -ResultSize Unlimited | Select PrimarySmtpAddress} | 
    Export-CSV C:\temp\resume.csv -Encoding UTF8 -NTI
4 Spice ups

Welcome

If you post code, please use the ‘Insert Code’ button. Please and thank you!

192033ab-bb8f-4032-88a5-8e2313af0344-codebutton_small.png

  • Neally Bot
1 Spice up

hahahaha Word strikes again! I’ve been updating the documentation in Word, but using Notepad++ for actually typing up the codes.

1 Spice up

Proper formatting would possibly have helped you see the issue, as Evan said, the parameter was not expected where it was

Import-CSV "C:\temp\users-output.csv" | 
ForEach-object {
    Get-Recipient -Identity $_.Name -ResultSize Unlimited | 
    Select-object PrimarySmtpAddress 
} | 
Export-CSV "C:\temp\resume.csv" -Encoding UTF8 -NoTypeInformation
1 Spice up

However, the command has not finished running, and there were only 200 names in the users-output.csv file.

I just checked the resume.csv file, and it’s got 11718 SMTP addresses. Something is wrong.

What does your CSV look like? Does it have a ‘name’ header?

Keep in mind you are querying Exchange 200 times, that might take a hot minute

e.g.

name
neally
jsepeta
$csv = import-csv "C:\temp\users-output.csv"

$report = 
foreach($row in $csv){
    $PSMTP = 
        try{
            Get-Recipient -Identity $row.Name -ResultSize Unlimited -ErrorAction Stop
        }
        catch{
            $Null
        }

    $PSMTP.PrimarySmtpAddress
}

$report | 
Export-CSV "C:\temp\resume.csv" -NoTypeInformation -Encoding UTF8

If it’s still running, you should not have your C:\temp\resume.csv file yet. Something is definitely wrong, as Get-Recipient -Identity $_.Name only gets data for that individual. Which begs the question, since you seem to be looking name-by-name from your .CSV, why -ResultSize Unlimited? Or are there any names in your .CSV that include wildcards?

If I do “Get-Recipient -Identity Jim6795” it just gets me, but “Get-Recipient -Identity J*” gets every mail-enabled object starting with ‘J’ in my AD.

For starters, this will dump all your SMTP Addresses:

Get-Recipient | Select Name, SAMAccountName, PrimarySMTPAddress

Using the -Filter parameter will let you cull the ones you don’t want:

Get-Recipient -Filter "RecipientType -eq 'UserMailbox'" | Select Name, SAMAccountName, PrimarySMTPAddress

(Note the Filter syntax: quotes around the whole Filter, apostrophes around the Value)

You can add -Filter parameters to narrow your scope:

Get-Recipient -Filter "(RecipientType -eq 'UserMailbox') -and (Alias -like '*')" | Select Name, SAMAccountName, RecipientType

You can use Get-Recipient jsepeta-rush | FL * to see all the data at your disposal.
How is it going for you? Made progress? More questions?

2 Spice ups

If you are a powershell noob, and this is your first command… um, congratulations! You’ve graduated!

I definitely wasn’t able to come up with a long pipeline command like this… until after quite a bit of … powershell experimental fun!

Do you have some other programming in your background?

Regards,

Jeff Cummings

MIS Technician