Odd CSV import behaviour

Hi all,

I’ve been using the import-csv cmdlet for years, to automate various jobs, mostly in AD.

Recently, as in v3 upward of Powershell, I have noticed that when importing data through a CSV, the data now looks all messed up.

I am attempting to import a very basic CSV containing a single column list of usernames. The usernames are all correct, and actually pulled from another script I ran and exported from Poweshell. I then need to add those users to a specific security group in AD. Here’s the two methods I’ve attempted:

Example script:

$users = import-csv "csvfile.csv"
$group = "securitygroupgoeshere"
foreach ($user in $users) {
add-adgroupmember -identity $group -Members $user
}

As you can see, very simple - I can also simplify this to one line:

import-csv "csvfile.csv" | foreach-object {add-adgroupmember -identity groupname -members $_ }

However, this doesn’t work. On both methods, they error out on each line in the CSV with the following:

Add-ADGroupMember : Cannot bind parameter 'Members'. Cannot convert value "@{akpm860=kelm617}" to type
"Microsoft.ActiveDirectory.Management.ADPrincipal". Error: "Cannot convert the "@{akpm860=kelm617}" value of type
"System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADPrincipal"."
At G:\Scripts\data moves\add-groupmembers.ps1:4 char:45
+ add-adgroupmember -identity $group -Members $user
+                                             ~~~~~
    + CategoryInfo          : InvalidArgument: [:] [Add-ADGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.AddADGroupM
   ember

Anyone have any idea why it is completely messing up the username from the CSV? That’s not what the imported file looks like at all! It’s almost like it’s taking the first line in the CSV and using that as a fieldname, e.g.

“user1” is ignored
“user2” becomes "@{user1=user2}

This is very strange behaviour. Have they changed something in the way Powershell parses CSVs?

Thanks,
Greg

Hey Greg,

Have you a header in your CSV file? The first line of the file is treated as the header. So you need to reference the header (a property when imported) when referring to the object (unless you also expand the property using select-object)

e.g. CSV file contents

hello
tim
how
are
you

PS 01/20/2015 11:40:09> $x = Import-Csv -Path C:\temp\hello.txt

PS 01/20/2015 12:01:44> $x

hello

tim
how
are

PS 01/20/2015 12:01:46> $x.hello
tim
how
are

Hmmm I had considered that, but I didn’t think you needed a header column for CSV’s, unless I’m thinking of text files!

Either way, seems to have done the trick - I moved the usernames down by 1 line and added the header as “username”.

I then made a small adjustment to the script:

$users = import-csv "csvfile.csv"
$group = "securitygroupgoeshere"
foreach [$user in $users] {
add-adgroupmember -identity $group -Members $user.username
}

et voila.

Thanks :slight_smile:

Hey there Greg,

You can also use the -Header parameter to add artificial headers so you don’t have to do it later in the pipe.

Example:

PS C:\Windows\System32\WindowsPowerShell\v1.0> Import-csv C:\Scripts\names.csv

William Everett Anderson

Bob J Boberson

PS C:\Windows\System32\WindowsPowerShell\v1.0> Import-csv C:\Scripts\names.csv -Header Names

Names

William Everett Anderson
Bob J Boberson

Have a good one!

Cheers Will. Something new learned today :slight_smile: