1

I have the following code:

$script:DatabaseFile = "C:\NewFolder\test.csv"

[string[]]$database = Get-Content -Path $script:DatabaseFile
[Collections.Generic.List[String]]$script:Database = $database

$script:Database | Out-File $script:DatabaseFile -Encoding ascii
$databaseAsCsvObject = Import-Csv $script:DatabaseFile
$databaseAsCsvObject | sort "Release Group","Email Address" | Export-Csv $script:DatabaseFile

$script:DatabaseFile contains the following:

Release Group,Email Address,Template ID,Time
CDP.HLO.1,[email protected],Template1,02:00
EDP.HLO.1,[email protected],Template2,03:00

This successfully sorts the csv file based on the first two columns.

How can I write the equivalent code without having to save the file, then import it as csv? Can I just get $script:Database as a "CSV" object, and then sort it, and then write it to file only once?

1 Answer 1

2
$script:Database = @(
[pscustomobject]@{"Release group"="EDP.HLO.1";"Email Address"="[email protected]";"Template ID"="Template2";"time"="03:00"},
[pscustomobject]@{"release group"="CDP.HLO.1";"Email Address"="[email protected]";"Template ID"="Template1";"time"="02:00"})

$sorted = $script:Database | Sort-Object "Release Group", "Email Address"

$script:DatabaseFile = "C:\data\test.csv"
$sorted | Export-csv -NoTypeInformation -Path $script:DatabaseFile

Okay so I have completely re-written on my first answer. First create your custom object with required headings. Create as many entries as you need. Sort the object, then export to csv :)

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, thanks for the awesome answer, I didn't even know you could do that! I've edited my question a bit, what if I have a List[string] already given to me, would I have to convert it to an array of custom objects? If so how, and then would it actually be easier to just save the file and then import it as csv like I was doing originally?
I guess it depends how the List[string] is given to you? Are you pulling from C:\newFolder\test.csv like your code suggests? If you are you won't have to create any custom objects, just import-csv. Can you give an example of where you are pulling the original data from? i.e what format
See my edited post. Although it's not very elegant, I think it's more efficient to just write to the file, then import that file as csv, then sort, then write to file again. Otherwise I assume I'd need to use a for loop to create the custom objects for each row in the csv file.
Thats true, you would need a for-loop. But from your code there, your reading from C:\newfolder\test.csv to get your values anyway? why dont you just use import-csv instead of get-content
Because after I read the file (get-content), I need to add to it, so I use get-content so that I'm able to treat it as a List[String] so I can easily add to it. Am I able to add to an object returned from import-csv?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.