Avatar of Justin Hannah
Justin Hannah
Flag for Australia asked on

PowerShell - Get-ADUser, foreach-object

Hi Experts,

Need a little help with PowerShell please.

Trying to get CanonicalName for each Object in my input csv file. It will have to export-csv also.

If I run this:

#Define files
$Source = 'C:\Temp\Exchange\AADSyncErrors\ExportErrors_2020_4_6_14_51_58_Filtered.csv'
$Output = 'C:\Temp\Exchange\AADSyncErrors\ExportErrors_2020_4_6_14_51_58_Filtered-Output.csv'

#Import CSV
Get-Content $Source | Foreach-object {
    Get-ADUser -Filter * -Properties CanonicalName,UserPrincipalName $_ | Export-CSV -Path $Output -Append 
}

Open in new window

I get error:
Get-ADUser : A positional parameter cannot be found that accepts argument 
User1

Get-ADUser : A positional parameter cannot be found that accepts argument 
User2

Get-ADUser : A positional parameter cannot be found that accepts argument 
User3

Open in new window

...

Thank you!
PowershellShell ScriptingActive Directory

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
oBdA

You can't pass input from the pipeline while at the same time querying for all users using the Filter argument.
You should be able to just pipe the test file to Get-ADUser:
#Define files
$Source = 'C:\Temp\Exchange\AADSyncErrors\ExportErrors_2020_4_6_14_51_58_Filtered.csv'
$Output = 'C:\Temp\Exchange\AADSyncErrors\ExportErrors_2020_4_6_14_51_58_Filtered-Output.csv'
Get-Content $Source | Get-ADUser -Properties CanonicalName, UserPrincipalName $_ | Export-CSV -Path $Output

Open in new window

If this doesn't work: which attribute exactly do you have in $Source that uniquely identifies the user?
Justin Hannah

ASKER
Hi oBda,

The $Source file has this:

IncomingObject.Mail
user1@contoso.com
user2@contoso.com
user3@contoso.com
user4@contoso.com
...

Thank you
ASKER CERTIFIED SOLUTION
Justin Hannah

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
EXPERT CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23