Hello Fellow PS users,<\/p>\n
I am trying to read from the csv as per below table. Obviously some values are missing and while I parse and execute and reexport it to csv, its not coming in the desired format, since the size of array is different for each line. How can i deal with empty columns and bind them correctly to the result?<\/p>\n
Project StartDate EndDate Activity Desciption Owner<\/p>\n
P1 10/10/23 10/10/25 Act1 Activity 1 Joe<\/p>\n
P2 10/10/23 10/10/25 Act1 Activity 1 Joe<\/p>\n
P3 Dave<\/p>\n
foreach($line in $config -split ‘\\s’ | ForEach-Object { “$_” }) \n{ \n#$iproject,$sd,$ed,$activity,$description,$owner = ($line -split ‘,’)[0,1,2,3,4,5]<\/p>\n
$Output = New-Object -TypeName psobject -Property @{<\/p>\n
‘Project’=$id \n‘SD’=$startDate \n‘EDate’=$endDate \n‘Activity’=$activity \n‘Description’=$description \n‘Owner’=$owner \n} \n$Result += $Output<\/p>\n
}<\/p>\n
$Result | Select * | export-csv -path ‘C:\\work\\MHVALUESSorted.csv’ -NoTypeInformation<\/p>","upvoteCount":11,"answerCount":11,"datePublished":"2023-05-04T12:46:14.000Z","author":{"@type":"Person","name":"inpswetrust","url":"https://community.spiceworks.com/u/inpswetrust"},"suggestedAnswer":[{"@type":"Answer","text":"
Hello Fellow PS users,<\/p>\n
I am trying to read from the csv as per below table. Obviously some values are missing and while I parse and execute and reexport it to csv, its not coming in the desired format, since the size of array is different for each line. How can i deal with empty columns and bind them correctly to the result?<\/p>\n
Project StartDate EndDate Activity Desciption Owner<\/p>\n
P1 10/10/23 10/10/25 Act1 Activity 1 Joe<\/p>\n
P2 10/10/23 10/10/25 Act1 Activity 1 Joe<\/p>\n
P3 Dave<\/p>\n
foreach($line in $config -split ‘\\s’ | ForEach-Object { “$_” }) \n{ \n#$iproject,$sd,$ed,$activity,$description,$owner = ($line -split ‘,’)[0,1,2,3,4,5]<\/p>\n
$Output = New-Object -TypeName psobject -Property @{<\/p>\n
‘Project’=$id \n‘SD’=$startDate \n‘EDate’=$endDate \n‘Activity’=$activity \n‘Description’=$description \n‘Owner’=$owner \n} \n$Result += $Output<\/p>\n
}<\/p>\n
$Result | Select * | export-csv -path ‘C:\\work\\MHVALUESSorted.csv’ -NoTypeInformation<\/p>","upvoteCount":11,"datePublished":"2023-05-04T12:46:14.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/1","author":{"@type":"Person","name":"inpswetrust","url":"https://community.spiceworks.com/u/inpswetrust"}},{"@type":"Answer","text":"
Do you you have an actual delimiter character between fields here or is it just a bunch of spaces? Can you show the original input data rather than what has already been manipulated?<\/p>","upvoteCount":2,"datePublished":"2023-05-04T12:49:01.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/2","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
Thank you for the response, Comma is the delimiter thats used.<\/p>","upvoteCount":0,"datePublished":"2023-05-04T12:49:55.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/3","author":{"@type":"Person","name":"inpswetrust","url":"https://community.spiceworks.com/u/inpswetrust"}},{"@type":"Answer","text":"\n\n
<\/div>\n
InPSwetrust:<\/div>\n
\nThank you for the response, Comma is the delimiter thats used.<\/p>\n<\/blockquote>\n<\/aside>\n
Can you show the original input data before it is manipulated? I see you splitting on spaces as the first thing in your code. It would be nice to have data without seemingly arbitrary spaces.<\/p>","upvoteCount":0,"datePublished":"2023-05-04T12:52:25.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/4","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
The csv is like this<\/p>\n
P1,10/10/23,10/10/25, Act1, Activity 1,Joe<\/p>\n
P2,10/10/23,10/10/25, Act1, Activity 1,Joe<\/p>\n
P3,Dave<\/p>","upvoteCount":0,"datePublished":"2023-05-04T12:52:28.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/5","author":{"@type":"Person","name":"inpswetrust","url":"https://community.spiceworks.com/u/inpswetrust"}},{"@type":"Answer","text":"
If you want to add headers, you can use the -Header parameter on import-csv:<\/p>\n
Import-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |\n\tExport-Csv outputFile.csv -NoType\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-05-04T13:00:28.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/6","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"Thank you, I can try importing the header, but he values for each rows are dynamically bound. First 2 rows are ok, but while processing the 3rd row, the last value from 2nd row is written to the 3rd row as well which is not the required behaviour. The 3rd row needs only the 1st and last columns to be populated.<\/p>","upvoteCount":0,"datePublished":"2023-05-04T13:07:56.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/7","author":{"@type":"Person","name":"inpswetrust","url":"https://community.spiceworks.com/u/inpswetrust"}},{"@type":"Answer","text":"
If you want to add headers but change your output delimiter, you can use -Delimiter on export-csv:<\/p>\n
# export with tab delimiter\nImport-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |\n\tExport-Csv outputFile.csv -NoType -Delimiter \"`t\"\n<\/code><\/pre>\nIf you want text output with aligned columns that’ll render the output feel useless for additional programming, you can use Format-Table:<\/p>\n
(Import-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |\n Format-Table -Auto | Out-String -Stream) -ne '' |\n Set-Content outputFile.txt\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-05-04T13:09:34.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/8","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"\n\n
<\/div>\n
InPSwetrust:<\/div>\n
\nThank you, I can try importing the header, but he values for each rows are dynamically bound. First 2 rows are ok, but while processing the 3rd row, the last value from 2nd row is written to the 3rd row as well which is not the required behaviour. The 3rd row needs only the 1st and last columns to be populated.<\/p>\n<\/blockquote>\n<\/aside>\n
I would have to imagine there are hidden newline characters that your text editor is not seeing. If the value Joe is showing up under Project on row 3, then there is a newline character between , and Joe on the second line.<\/p>","upvoteCount":0,"datePublished":"2023-05-04T13:19:10.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/9","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
If we are dealing with newline characters that are not at the end of a line, then we will need a way to remove those without impacting the real end of a line. For example, if all end of lines have CRLF but you have sporadic LF without CR throughout, you can simply replace those single LF characters:<\/p>\n
(Get-Content inputFile.csv -Raw) -split '\\r\\n' -replace '\\n' -ne '' |\n ConvertFrom-Csv -Header Project,StartDate,EndDate,Activity,Description,Owner |\n Export-Csv outputfile.csv -NoType\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-05-04T13:33:27.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/10","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"personally I typically dump CSVs into a hashtable using import-csv. makes it a lot easier to work with normally, however as stated above headers are required for this as well. gives you a nice “no effort” way to call the items as well when its done<\/p>\n
for reference, i use it with a list of software… so i specify the below as “Software” without the quotes<\/p>\n
$CSV = “”<\/p>\n
$CSVContent = Import-csv $csv | Group-object -AsHashTable -Property <\/p>\n
so using my software reference above i could call something like $<\/p>\n
$CSVContent[“Adobe Air”] .TaskSequenceVariable <— another header from the csv file and it would return the variable used in my task sequence to detect if it should install adobe air for that software load.<\/p>","upvoteCount":0,"datePublished":"2023-05-05T00:47:43.000Z","url":"https://community.spiceworks.com/t/powershell-csv-columns-with-no-value/951287/11","author":{"@type":"Person","name":"slipz1622","url":"https://community.spiceworks.com/u/slipz1622"}}]}}
Hello Fellow PS users,
I am trying to read from the csv as per below table. Obviously some values are missing and while I parse and execute and reexport it to csv, its not coming in the desired format, since the size of array is different for each line. How can i deal with empty columns and bind them correctly to the result?
Project StartDate EndDate Activity Desciption Owner
P1 10/10/23 10/10/25 Act1 Activity 1 Joe
P2 10/10/23 10/10/25 Act1 Activity 1 Joe
P3 Dave
foreach($line in $config -split ‘\s’ | ForEach-Object { “$_” })
{
#$iproject,$sd,$ed,$activity,$description,$owner = ($line -split ‘,’)[0,1,2,3,4,5]
$Output = New-Object -TypeName psobject -Property @{
‘Project’=$id
‘SD’=$startDate
‘EDate’=$endDate
‘Activity’=$activity
‘Description’=$description
‘Owner’=$owner
}
$Result += $Output
}
$Result | Select * | export-csv -path ‘C:\work\MHVALUESSorted.csv’ -NoTypeInformation
11 Spice ups
Do you you have an actual delimiter character between fields here or is it just a bunch of spaces? Can you show the original input data rather than what has already been manipulated?
2 Spice ups
Thank you for the response, Comma is the delimiter thats used.
Can you show the original input data before it is manipulated? I see you splitting on spaces as the first thing in your code. It would be nice to have data without seemingly arbitrary spaces.
The csv is like this
P1,10/10/23,10/10/25, Act1, Activity 1,Joe
P2,10/10/23,10/10/25, Act1, Activity 1,Joe
P3,Dave
If you want to add headers, you can use the -Header parameter on import-csv:
Import-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |
Export-Csv outputFile.csv -NoType
Thank you, I can try importing the header, but he values for each rows are dynamically bound. First 2 rows are ok, but while processing the 3rd row, the last value from 2nd row is written to the 3rd row as well which is not the required behaviour. The 3rd row needs only the 1st and last columns to be populated.
If you want to add headers but change your output delimiter, you can use -Delimiter on export-csv:
# export with tab delimiter
Import-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |
Export-Csv outputFile.csv -NoType -Delimiter "`t"
If you want text output with aligned columns that’ll render the output feel useless for additional programming, you can use Format-Table:
(Import-Csv inputFile.csv -Header Project,StartDate,EndDate,Activity,Description,Owner |
Format-Table -Auto | Out-String -Stream) -ne '' |
Set-Content outputFile.txt
InPSwetrust:
Thank you, I can try importing the header, but he values for each rows are dynamically bound. First 2 rows are ok, but while processing the 3rd row, the last value from 2nd row is written to the 3rd row as well which is not the required behaviour. The 3rd row needs only the 1st and last columns to be populated.
I would have to imagine there are hidden newline characters that your text editor is not seeing. If the value Joe is showing up under Project on row 3, then there is a newline character between , and Joe on the second line.
If we are dealing with newline characters that are not at the end of a line, then we will need a way to remove those without impacting the real end of a line. For example, if all end of lines have CRLF but you have sporadic LF without CR throughout, you can simply replace those single LF characters:
(Get-Content inputFile.csv -Raw) -split '\r\n' -replace '\n' -ne '' |
ConvertFrom-Csv -Header Project,StartDate,EndDate,Activity,Description,Owner |
Export-Csv outputfile.csv -NoType
slipz1622
(Slipz1622)
May 5, 2023, 12:47am
11
personally I typically dump CSVs into a hashtable using import-csv. makes it a lot easier to work with normally, however as stated above headers are required for this as well. gives you a nice “no effort” way to call the items as well when its done
for reference, i use it with a list of software… so i specify the below as “Software” without the quotes
$CSV = “”
$CSVContent = Import-csv $csv | Group-object -AsHashTable -Property
so using my software reference above i could call something like $
$CSVContent[“Adobe Air”] .TaskSequenceVariable <— another header from the csv file and it would return the variable used in my task sequence to detect if it should install adobe air for that software load.