I am working to process a raw CSV file given to my team via SFTP and update the format to the correct format for our system needs. I've been requested to script this via PowerShell so the script can be scheduled for daily runs on a non-technical team member's local machine.
To rename the column headings, I've routinely used the "Select-Object" expression in other projects. However, for some reason it is not working in this use case. The script runs to completion and saves the final file, but is maintaining the original headings.
Using the Select-Object approach to rename two columns from "Email" to "EMAIL_ADDRESS" and "EmployeeID" to "SrchKeyEMPLID" is not working. The input csv file is formatted as:
| EmployeeID | |
|---|---|
| 123 | [email protected] |
| 456 | [email protected] |
The code is:
$csvPath = 'file_path_raw_file.csv'
$tempPath = 'file_dropping_null_rows.csv' ##needed for validations
$pitstopPath = 'file_dropping_unneeded_cols.csv'
$finalPath = 'final_file_sys_load.csv'
Import-Csv $csvPath | Where-Object {
$_.PSObject.Properties.Value -notcontains $null -and $_.PSObject.Properties.Value -notcontains ""
} | Export-Csv $tempPath -NoTypeInformation
Import-Csv $tempPath | Select-Object Email,EmployeeID | Export-Csv $pitstopPath -NoTypeInformation
$formatFile = Import-Csv $pitstopPath
$formatFile | Add-Member -MemberType NoteProperty -Name 'EMAIL1_NOTIFY' -Value 'Y'
$formatFile | Select-Object -Property @{Name = 'EMAIL_ADDRESS'; Expression ={$_.Email}},@{Name='SrchKeyEMPLID';Expression={$_.EmployeeID}}
$formatFile | Export-Csv -path $finalPath -NoTypeInformation
$checksBals = Import-Csv $finalPath
$header_values = $checksBals[0].PSObject.Properties.name
$header_values
Add-Membershould probably come after theSelect, and in theSelectexpressions it should be$_.Emailnot just$.Email. Same with EmployeeID.$.should be$_., using the automatic$_variable. I've corrected the typo to focus on the larger issues, as addressed in Santiago's answer.