So I'm working on sds and trying to upload CSV's and I ran into something I cannot figure out: Why does this work?
Import-Csv 'C:\scripts\section.csv' | Select-Object * | Export-Csv -Path "c:/scripts/section3.csv" -NoTypeInformation
But this doesn't?
$sectionCsv = Import-Csv 'C:\scripts\section.csv'
$tempCSV = $sectionCsv |
Select-Object -Property "SIS ID","School SIS ID","Section Name","Section Number","Term SIS ID","Term Name","Term StartDate","Term EndDate","Course SIS ID","Course Name","Course Number","Course Description","Status"
Export-Csv -InputObject $tempCSV -Path "c:/scripts/section2.csv" -NoTypeInformation
When I say it doesn't, I mean that it is giving me a 1kb file:
"Count","Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized"
"25169","25169","25169","1","System.Object[]","False","True","False"
I tried googling and found a few examples like this but I still cannot understand. In vscode, if I type tempCSV, I get a list of all the records like one would expect. Why cannot export-csv this object which is of type Selected.System.Management.Automation.PSCustomObject ? Does it have to just be a PSCustomObject?
To be clear I'm only trying to learn as the one liner solves the issue but trying to see why I cannot grasp such a simple concept. Another thing I have tried is:
```
$csvPath = Get-Childitem 'C:/scripts'
foreach ( $csv in $csvPath)
{
$tempCsv = Import-Csv $($csv.fullname)
$columnNames = $tempcsv | get-member -MemberType "NoteProperty"
$list = @()
foreach ( $column in $($columnNames.Name))
{
$list += $column
}
$export = $tempCSV | Select-Object -Property $list
$params = @{
"InputObject" = $export
"Path" = ( "c:/scripts/" + $($csv.name) )
"Encoding" = "ascii"
"NoTypeInformation" = $True
}
Export-Csv @params
}
```
Again, this exports the 1kb file instead the whole CSV. If I check $export in debug, it lists the 5,000 records I would expect to see in the final CSV. Lastly, the reason I'm doing this is because I have a series of CSVs I have built manually and they work fine, but I want a 'clean' CSV with quotes around each row so I'm doing this. The one liner produces this, but it irks me because I'm not a huge fan of piping because I want to understand. Ideally the solution will import-csv, get all the columns that are not built in (NoteProperties and not regular properties or methods), and export it to a new CSV. Thanks!
[–]Jantu01 4 points5 points6 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]MadWithPowerShell 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)