0

I have a CSV file that contains some column. In those columns, some characters are in German language.

Sample Data

Test01ñ
DúSibaagh01
ËTheroË01Ë
DMrçzundaljak01
PçSchpaglawarz01ç

Script

import-csv D:\users.csv | 
foreach {
 If($_.Samaccountname -contains "ñ") {
 $_.Samaccountname -replace "ñ","N"
 }
 } | export-csv D:\Users_myfile.csv

Unfortunately, script doesn't replace ñ with N If condition always false. I tried with -match and -like keyword. But none of them is working in this case.

Any advise what to do.

Roxx
  • 3,738
  • 20
  • 92
  • 155
  • 1
    Those special characters `ñ ú Ë ç` are definitely **NOT** German ones! More like French and Spanish .... – marc_s Dec 24 '18 at 10:39
  • 1
    this looks like it answers your question ... active directory - How remove accents in PowerShell? - Stack Overflow — https://stackoverflow.com/questions/7836670/how-remove-accents-in-powershell – Lee_Dailey Dec 24 '18 at 10:40
  • @Lee_Dailey Thanks for your help. Let me try. – Roxx Dec 24 '18 at 11:19
  • @Ironic - you are most welcome! glad to have helped a bit ... [*grin*] – Lee_Dailey Dec 24 '18 at 11:30

2 Answers2

0

-contains is a collection operator - it doesn't work on strings. Use String.Contains() instead:

Import-Csv D:\users.csv |ForEach-Object {
    if($_.Samaccountname.Contains("ñ")) {
        $_.Samaccountname = $_.Samaccountname -replace "ñ","N"
    }
    $_
} |Export-Csv D:\Users_myfile.csv
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0
  • Im-/Export-Csv will inevitably double quote all columns.
  • if the encoding is - and should stay - in UTF8, append -Encoding UTF8 on import and export
  • to remove diacritics only on specified columns, you'll have to iterate the rows and apply the Remove-Diacritics function on just those columns.

Given a sample users.csv:

"UserName","LastName"
"Test01ñ","Test01ñ"
"DúSibaagh01","DúSibaagh01"
"ËTheroË01Ë","ËTheroË01Ë"
"DMrçzundaljak01","DMrçzundaljak01"
"PçSchpaglawarz01ç","PçSchpaglawarz01ç"

This script:

## Q:\Test\2018\12\24\SO_53912246.ps1

function Remove-Diacritics {
  param ([String]$src = [String]::Empty)
   # Source: https://stackoverflow.com/a/7840951/6811411
   $normalized = $src.Normalize( [Text.NormalizationForm]::FormD )
    $sb = new-object Text.StringBuilder
    $normalized.ToCharArray() | % {
        if( [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne
            [Globalization.UnicodeCategory]::NonSpacingMark ) {
            [void]$sb.Append($_)
        }
    }
    $sb.ToString()
}


$CsvData = Import-csv .\Users.csv -Encoding UTF8

$CsvData | ForEach-Object {
    $_.UserName = Remove-Diacritics $_.UserName
}
$CsvData
$CsvData | Export-Csv .\New_Users.csv -Encoding UTF8 -NoTypeInformation

will create this output:

UserName          LastName
--------          --------
Test01n           Test01ñ
DuSibaagh01       DúSibaagh01
ETheroE01E        ËTheroË01Ë
DMrczundaljak01   DMrçzundaljak01
PcSchpaglawarz01c PçSchpaglawarz01ç