Q: Powershell Remove Quotes from Header Row of CSV Yes, I know if goes against the standards of the CSV, but I need to feed data to an application that will not handle quotes in the header of the csv. However, it needs the quotes in the the rows of data.
My process takes a complete mutli-row csv file, and splits in into a single csv for each row of data. This is to help isolate data issues on import as the target system has poor error handling. This process works. I then found the the quotes were an issue, I changed the script to remove all quotes and then was informed that only the quote from the header row need to be removed.
I am at a loss on how I can accomplish this. I would like to write the header without the quote and include in the data rows. Some other ideas would be to write the CSV with quotes then in a second step strip the quotes from the first row. I have considered writing the file without the header and prepending a file containing the header to each csv created, but the columns can vary each time.
Any suggestions would be appreciated. Code below:
#This script reads the combined csv file  and splits it into multiple csv files,
#one for each row is in the sheet. Place original in the input folder under the folder this script is
#located in. Make sure there is an output folder. Script will then place multiple files in output folder.

Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "CSV (*.csv)| *.csv"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}



#Set Working directory
Set-Location c:\documents\powershell\;

#remove existing files
Remove-Item .\output\*.csv

# variable used to store the path of the source CSV file - calls function specifying default directory
$sourceCSV = Get-FileName "C:\documents\powershell" ;
#echo $sourceCSV;

# get max number of rows
$maxrows = Import-Csv $sourceCSV | Measure-Object | Select-Object -expand count;
#echo $maxrows;

# variable used to advance the number of the row from which the export starts
$startrow = 0 ;

# counter used in names of resulting CSV files
$counter = 1 ;


while ($startrow -lt $maxrows)
{

Import-CSV $sourceCSV | select-object -skip $startrow -first 1 | convertto-csv -NoType | % { $_ -replace '"', ""} | out-file ".\output\$($counter).csv" -fo -en ascii;

#Import-CSV $sourceCSV | select-object -skip $startrow -first 1 | Export-CSV ".\output\$($counter).csv" -NoClobber -NoType;


$startrow += 1 ;

$counter++ ;

}

After input - the solution was:
Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "CSV (*.csv)| *.csv"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}



#Set Working directory
Set-Location c:\documents\powershell\;

#remove existing files
Remove-Item .\output\*.csv

# variable used to store the path of the source CSV file - calls function specifying default directory
$sourceCSV = Get-FileName "C:\documents\powershell" ;


$maxrows = Import-Csv $sourceCSV | Measure-Object | Select-Object -expand count;
#csv does not count header row
$maxrows = $maxrows+1; 


# variable used to advance the number of the row from which the export starts - set to 1 since we are including first row everytime
$startrow = 1 ;


$counter = 1 ;

$csv = Get-Content $sourceCSV;

while ($startrow -lt $maxrows)
{

# import of however many rows you want the resulting CSV to contain starting from the $startrow position and export of the imported content to a new file

($csv[0] -replace '"',''),$csv[$startrow] | Set-Content ".\output\$($counter).csv" -Force

$startrow += 1 ;

$counter++ ;

}

