3

I have written a script for my MySQL database with the Powershell ISE, which creates a backup for me with the help of the MySQL dump tool. When I run the script in the PowerShell ISE, everything works. If I execute the same script now in the normal PowerShell, he does not show me the German umlauts correctly.

Here is my script:

# delete everything older than 30 days
foreach ($ordner in (ls D:\Backup -Depth 0)) 
{
    if ($ordner.LastWriteTime.Date -lt (Get-Date).AddDays(-30).Date)
    {
        rm -Recurse ("D:\Backup\"+ $ordner.Name)
    }
}


mkdir ("D:\Backup\" + (Get-Date -Format "yyyy_MM_dd") + "\Datenbank")

C:\xampp\mysql\bin\mysqldump.exe -uroot --default-character-set=latin1 --opt MY_DATABASE > ("D:\Backup\"+(Get-Date -Format "yyyy_MM_dd") + "\Datenbank\backup.sql")

However, I need the normal PowerShell for the automated execution and the script.

How can I fix the problem that the German umlauts are displayed in the normal PowerShell correctly?

mklement0
  • 382,024
  • 64
  • 607
  • 775
groebi
  • 33
  • 5

1 Answers1

3

The Windows PowerShell ISE differs from regular (conhost.exe) PowerShell console windows in that it interprets the output from external programs (such as mysqldump.exe) as encoded based on the active ANSI code page (e.g., Windows-1252 on US-English systems) - which is what --default-character-set=latin1 in your command requests.

By contrast, regular PowerShell console windows default to the active OEM code page (e.g., 437 on US-English systems).

It is the encoding reported by [console]::OutputEncoding that determines how PowerShell interprets the output from external programs (though for mere display output that may not matter).

Therefore, you have two options:

  • Adjust the --default-character-set option to match the code page reported by [console]::OutputEncoding - assuming MYSQL supports it (this documentation suggests that the US-English OEM code page 437 is not supported, for instance).

  • Adjust [console]::OutputEncoding to (temporarily) match the specified --default-character-set option:

    • [console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(1252)

In general, it is (Get-Culture).TextInfo.ANSICodePage / (Get-Culture).TextInfo.OEMCodePage that reports the a given system's active ANSI / OEM code page number.

mklement0
  • 382,024
  • 64
  • 607
  • 775