I'm trying to split a CSV in to 2 based on the values in the first column. The newly created CSVs cannot have a column name and this is where the problem arises. When I remove the headers, all the values go into the first column.
In order to split the CSV based on the first column value I've had to assign headers. The 2 new files are created successfully with the correct values, however my final step is to remove the headers as the application used to process the CSV will not accept column names.
Once I've removed the headers, all the values are no longer separated by the comma and as a result are all in the first column. How can I get around this?
if (!(Test-Path X:)) {
$net = New-Object -comobject Wscript.Network
$net.MapNetworkDrive("X:","**Path**")
}
$file = Get-ChildItem -Path "**Path**" | Where-Object {
$_.LastWriteTime -gt (Get-Date).AddHours(-10) -and
$_.Name -like '*.CSV*'
}
if ($file) {
$filename = $file.FullName
$date = (Get-Date).ToString("yyyyMMdd")
$ExportPathJUO = "**Path**$date"
$ExportPathWDA = "**Path**$date"
##Create Seperate Files in Staging Folder
$csv = Import-Csv $filename -Header A,B,C,D,E,F,G
$IDS = $csv | select -ExpandProperty A -Unique
foreach ($ID in $IDS) {
$csv | where {
$_.A -like '*0000009*'
} | Export-Csv $ExportPathJUO -NoTypeInformation
}
foreach ($ID in $IDS) {
$csv | where {
$_.A -like '*0000007*'
} | Export-Csv $ExportPathWDA -NoTypeInformation
}
##Remove Headers
(Get-content $ExportPathJUO) |
select -Skip 1 |
Out-File -FilePath $ExportPathJUO -Force
(Get-content $ExportPathWDA) |
select -Skip 1 |
Out-File -FilePath $ExportPathWDA -Force
}
$fileas a single item instead of a possible array better iterated with a foreach.