This is simple enough:
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> ls .\test.csv
Directory: C:\Users\saunders\Desktop\data
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023-01-31 1:38 PM 640 test.csv
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> cat .\test.csv
UserPrincipalName,"DisplayName","Title","UserType","IsLicensed"
LeeG@lazydev.com,"Lee Gu","jr engineer","Member","True"
MeganB@lazydev.com,"Megan Bowen","recruiter","Member","True"
GradyA@lazydev.com,"Grady Archie","sr engineer","Member","True"
MiriamG@lazydev.com,"Miriam Graham","Director","Member","True"
openmailbox@lazydev.com,"openmailbox",,"Member","False"
JohannaL@lazydev.com,"Johanna Lorenz","Senior Engineer","Member","True"
JoniS@lazydev.com,"Joni Sherman","recruiter","Member","False"
AlexW@lazydev.com,"Alex Wilber","Marketing Assistant","Member","True"
IsaiahL@lazydev.com,"Isaiah Langer","Sales Rep","Member","True"
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> $test = Import-CSV .\test.csv
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> $test[3]
UserPrincipalName : MiriamG@lazydev.com
DisplayName : Miriam Graham
Title : Director
UserType : Member
IsLicensed : True
PS C:\Users\saunders\Desktop\data>
But how would CSV formatted data be obtained from a formatted text file?
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> $records = Get-Content .\records.txt
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> $records
UserPrincipalName : LeeG@lazydev.com
DisplayName : Lee Gu
Title : jr engineer
UserType : Member
IsLicensed : True
UserPrincipalName : MeganB@lazydev.com
DisplayName : Megan Bowen
Title : recruiter
UserType : Member
IsLicensed : True
UserPrincipalName : GradyA@lazydev.com
DisplayName : Grady Archie
Title : sr engineer
UserType : Member
IsLicensed : True
UserPrincipalName : MiriamG@lazydev.com
DisplayName : Miriam Graham
Title : Director
UserType : Member
IsLicensed : True
UserPrincipalName : openmailbox@lazydev.com
DisplayName : openmailbox
Title :
UserType : Member
IsLicensed : False
UserPrincipalName : JohannaL@lazydev.com
DisplayName : Johanna Lorenz
Title : Senior Engineer
UserType : Member
IsLicensed : True
UserPrincipalName : JoniS@lazydev.com
DisplayName : Joni Sherman
Title : recruiter
UserType : Member
IsLicensed : False
UserPrincipalName : AlexW@lazydev.com
DisplayName : Alex Wilber
Title : Marketing Assistant
UserType : Member
IsLicensed : True
UserPrincipalName : IsaiahL@lazydev.com
DisplayName : Isaiah Langer
Title : Sales Rep
UserType : Member
IsLicensed : True
PS C:\Users\saunders\Desktop\data>
So that the data for each record is transposed and then written to a row in a CSV file. No doubt there's a term for this inverse operation. It doesn't have to be CSV per se, it's just that the above sample originates as CSV.
Please do correct any terminological errors.
as it stands, no the $records object cannot itself be directly exported back to CSV with:
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> $records = Get-Content .\records.txt
PS C:\Users\saunders\Desktop\data>
PS C:\Users\saunders\Desktop\data> Export-Csv $records
Export-Csv : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.
At line:1 char:12
+ Export-Csv $records
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand
PS C:\Users\saunders\Desktop\data>
as it will first have to parsed somehow.
With Windows PowerShell specifically, you have the option of using ConvertFrom-String
's template-based parsing capability:
# define a template using the data from 2 consecutive records from the input data
$recordTemplate = @'
UserPrincipalName : {UserPrincipalName*:LeeG@lazydev.com}
DisplayName : {DisplayName:Lee Gu}
Title : {Title:jr engineer}
UserType : {UserType:Member}
IsLicensed : {IsLicensed:True}
UserPrincipalName : {UserPrincipalName*:MeganB@lazydev.com}
'@
# ConvertFrom-String will use the example(s) in the template to infer the format of the remaining records
Get-Content .\records.txt | ConvertFrom-String -TemplateContent $recordTemplate