Hi there,
I'm using Import-Csv to read an addressbook in CSV text format, one entry per line, then I'm do some validation on each entry and write it back out as a CSV file using the following PowerShell line (I also need to remove the default double-quoting as well as the header line) :
$ValidData | Select-Object Lastname, Firstname, Email, Phone, Office, Department | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | %{ $_ -replace '"', "" } | Select-Object -Skip 1 | Out-File -FilePath $OutputFileEC -Encoding Default
The speed and perfomance are acceptable but the PowerShell looks very long to me. Is there a (much) better way to do the same thing please or is it good enough?
Thanks,
- Alan.
Checking PS code to write object array to CSV file please.
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Checking PS code to write object array to CSV file please.
No that is about the simplest way.
You could also just format the rows to a file:
Sometimes clarity is more useful than simplicity.
You could also just format the rows to a file:
Sometimes clarity is more useful than simplicity.
- $ValidData |
- ForEach-Object{
- '{0},{1},{2},{3},{4},{5}' -f $_.Lastname, $_.Firstname, $_.Email, $_.Phone, $_.Office, $_.Department
- } | Out-File $OutputFileEC