Link to home
Start Free TrialLog in
Avatar of beardog1113
beardog1113Flag for China

asked on

exclude quotation mark while powershell a CSV file

hello experts
my example CSV file is below
group,owner
group1,"alice,bob"
group2,"jack,david"

while i call the CSV file via powershell, it with quotation mark so that command can't recognize it, my question is how to exclude the quotation mark while i running command with call such CSV file.

thank you
Avatar of Prashant Girennavar
Prashant Girennavar
Flag of India image

May be simple solution to this is to remove the quotation mark in a csv file.

Just open up the CSV file and do a cntl+h (Find and replace funtion), replace "" mark with nil , so that the file have no quotation ,

Then try calling csv.....

Thanks,

-Prashant Girennavar.
Avatar of beardog1113

ASKER

hello
what is nil as you mentioned?
or what will the CSV file looks like from my example?

thank you
Avatar of oBdA
oBdA

Please describe your problem more precisely. Powershell has no problems importing a csv with mixed quoted and non-quoted fields. The resulting properties will be stripped of surrounding quotes.
PS C:\Temp> Get-Content .\TestQuotes.csv
group,owner
group1,"alice,bob"
group2,"jack,david"
PS C:\Temp> Import-Csv .\TestQuotes.csv | ft -au

group  owner
-----  -----
group1 alice,bob
group2 jack,david


PS C:\Temp> (Import-Csv .\TestQuotes.csv)[0].Owner
alice,bob
PS C:\Temp>

Open in new window

hello oBdA
i am using Microsoft online module of powershell to do the managing work, for example
Import-Csv c:\test.csv | %{Set-DistributionGroup -Identity $_."group" -ManagedBy $_.owner}
i will get error it will same can't not found "alice,bob", that is because there is quotation mark at begin and end of owner column, if i try configure it for an individual group, without any problem, for example:
Set-DistributionGroup -Identity group1 -ManagedBy alice,bob
hope you understand what i am meaning.

thank you
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello oBdA
thats great your example fixed my issue, could you also explain how ($_.owner).Split(',') exclude the quotation mark from the CSV file then into cmdlet?

thank you
that s great, thank you
Again: this has nothing to do with the quotation marks in the csv file.
When importing a csv, all fields are imported as string (because PS has no knowledge which type a field could have). So when you're importing a field "alice,bob", it will result in a string property and the content alice,bob (without surrounding quotes - see my very first post). This is the same as $a = "Some,String".
In your script, you were passing this string directly to the ManagedBy argument, and since there is no account 'alice,bob', the command failed.
Note the difference between
Set-DistributionGroup -ManagedBy "alice,bob" (which is the same as the command you created in your initial script)
and
Set-DistributionGroup -ManagedBy "alice","bob" (which is the same as the command you entered manually, except that I added quotes for clarity).

The Split() method on the string 'alice,bob' turns it back into a an array with two separate strings, "alice" and "bob".
i am clear now, thank you very much, nice day.