I have tried your steps based on some data. I have deleted some columns so it is easier to digest. First the original Powershell script:
# Import the data from the CSV file
$data = Import-Csv -Path .\broken.csv
# Display the names of the columns
$data[0].PSObject.Properties.Name
# Select the desired columns
$selectedData = $data | Select-Object 'Licenses', 'User principal name'
# Export the selected data to a new Excel file
$selectedData | Export-Excel -Path .\users.xlsx -WorksheetName "Sheet1" -AutoSize
The original broken.csv (opened in notepad)
Block credential,City,Country/Region,Department,Display name,First name,Last name,Licenses,Office,User principal name,When created
"False,,Netherlands,""Reizen-TH Office, MT & HRM"",john doe,john,doe,Enterprise Mobility + Security E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,""Reizen-TH Office, MT & Customer Service"",alex pete,alex,pete,Microsoft Power Automate Free,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,""Reizen-TH Office, MT & HRM"",john doe,john,doe,Microsoft 365 E5 Security for EMS E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,""Reizen-TH Office, MT & Customer Service"",alex pete,alex,pete,Microsoft 365 E3,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z"
After I run my original script: no data in the xlsx, only the 2 column names: Licenses and User Principal Name.
Then i tried this:
(Get-content .\broken.csv) -replace '""','"' | Set-Content .\fixed.csv
Outputof the fixed.csv in notepad is:
Block credential,City,Country/Region,Department,Display name,First name,Last name,Licenses,Office,User principal name,When created
"False,,Netherlands,"Reizen-TH Office, MT & HRM",john doe,john,doe,Enterprise Mobility + Security E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,"Reizen-TH Office, MT & Customer Service",alex pete,alex,pete,Microsoft Power Automate Free,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,"Reizen-TH Office, MT & HRM",john doe,john,doe,Microsoft 365 E5 Security for EMS E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z"
"False,,Netherlands,"Reizen-TH Office, MT & Customer Service",alex pete,alex,pete,Microsoft 365 E3,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z"
If I use the fixed.csv, still no data, only the column names.
Now I saw that that each line begins with a " and ends with " . I removed that with:
# Get the content of the CSV file
$lines = Get-Content -Path .\fixed.csv
# Process each line
foreach ($line in $lines) {
# Remove the first and last double quotes
$line = $line.TrimStart('"').TrimEnd('"')
# Write the modified line to a new CSV file
Add-Content -Path .\fixed_no_quotes.csv -Value $line
}
And now I got this:
Block credential,City,Country/Region,Department,Display name,First name,Last name,Licenses,Office,User principal name,When created
False,,Netherlands,"Reizen-TH Office, MT & HRM",john doe,john,doe,Enterprise Mobility + Security E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z
False,,Netherlands,"Reizen-TH Office, MT & Customer Service",alex pete,alex,pete,Microsoft Power Automate Free,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z
False,,Netherlands,"Reizen-TH Office, MT & HRM",john doe,john,doe,Microsoft 365 E5 Security for EMS E5,Hoofdkantoor Den Haag,john.doe@domainname.nl,2020-06-17 13:19:04Z
False,,Netherlands,"Reizen-TH Office, MT & Customer Service",alex pete,alex,pete,Microsoft 365 E3,Hoofdkantoor Den Haag,alex.pete@domainname.nl,2020-06-17 13:19:04Z
And this csv files works! But is that a good workaround, to delete the first and lost " ? What If in some cases have no first and last " ?