Wochentag und Monat mit PowerShell in unterschiedlicher Sprache ausgeben

Wird mit PowerShell über Get-Date das aktuelle Datum abgefragt nutzt PowerShell die regionalen Einstellungen von Windows.

PowerShell

Die Einstellungen lassen sich mit PowerShell über Get-Culture auswerten und in den Regionaleinstellungen von Windows sind die detaillierten Formate abgebildet.

Lokale Konfiguration für regionale Einstellungen
Lokale Konfiguration für regionale Einstellungen

Nun möchte ich eine deutsche Bezeichung ausgeben.

PowerShell
 Get-Date -Format "dddd, dd.MM.yyyy" OR
 Get-Date -Format "dddd, dd. MMMM yyyy"

Durch meine englische Konfiguration stimmt die Bezeichnung für Wochentag/Monat nicht.

PowerShell

Statt die Regionaleinstellungen anzupassen kann ich PowerShell mitgeben welche Sprache es (einmalig) nutzen soll.

  • Für den Wochentag
PowerShell
$RegionalCulture = New-Object System.Globalization.CultureInfo("de-CH")
$DayName = $RegionalCulture.DateTimeFormat.GetDayName((Get-Date).DayOfWeek)
$Date = ("$DayName, " + (Get-Date -format "dd.MM.yyyy"))
$RegionalCulture.TextInfo.ToTitleCase($Date) # to capitalize the first character of the day
PowerShell
  • Für Wochentag und Monat

Mit ein wenig Zusatz passt es den Monatsnamen ebenfalls auf die deutsche Bezeichnung an.

PowerShell
$RegionalCulture = New-Object System.Globalization.CultureInfo("de-CH")
$DayName = $RegionalCulture.DateTimeFormat.GetDayName((Get-Date).DayOfWeek)
$MonthName = $RegionalCulture.DateTimeFormat.GetMonthName(((Get-Date).Month))
$Date = ("$DayName, " + (Get-Date -format "dd. MMMMM yyyy")).Replace((Get-Date -format MMMM),$MonthName)
$RegionalCulture.TextInfo.ToTitleCase($Date) # to capitalize the first character of day and month
PowerShell

Durch die einmalige Anpassung der Sprache kann es auf viele Sprachen verändert werden. Die regionalen Sprachcodes listet Microsoft unter anderem hier. Für die Schweiz unterstützt es auch Rätoromanisch.

PowerShell
$RegionalCulture = New-Object System.Globalization.CultureInfo("rm-CH")
$DayName = $RegionalCulture.DateTimeFormat.GetDayName((Get-Date).DayOfWeek)
$MonthName = $RegionalCulture.DateTimeFormat.GetMonthName(((Get-Date).Month))
$Date = ("$DayName, " + (Get-Date -format "dd. MMMMM yyyy")).Replace((Get-Date -format MMMM),$MonthName)
$RegionalCulture.TextInfo.ToTitleCase($Date) # to capitalize the first character of day and month
PowerShell
Share this post:

About the Author

Tobias Asböck

Tobias Asböck

Tobias is a Senior System Engineer with more than 10 years of professional experience with Microsoft 365 products such as SharePoint Online, OneDrive for Business, Teams Collaboration, Entra ID, Information Protection, Universal Print, and Microsoft 365 Licensing. He also has 15+ years of experience planning, administering, and operating SharePoint Server environments. In his spare time, Tobias is busy with updates in the Microsoft 365 world or on the road with his road bike and other sports activities. If you have additional questions, please contact me via LinkedIn or support@topedia.com.

No comments yet