In the previous tip we illustrated how Get-Date can take a format string and convert DateTime values to strings. The string conversion always uses your local language though. That might not always be what you need. Let’s check out the problem, and a solution for it:
Here is an example outputting the weekday name for Christmas Eve in 2018:
$christmasEve = Get-Date -Date '2018-12-24' Get-Date -Date $christmasEve -Format '"Christmas Eve in" yyyy "will be on" dddd.'
Clearly, the conversion was done on a German system, so the result uses the German name of the weekday:
Christmas Eve in 2018 will be on Montag.
If your script was supposed to create output for a different language, you might want to output the days of the week in English (or any other language). To take control over the language, you need to be aware of two things: first, the formatting option provided by Get-Date and -Format is simply a wrapper around the generic .NET method ToString(), so you can as well run this and get the same result:
$christmasEve = Get-Date -Date '2018-12-24' $christmasEve.ToString('"Christmas Eve in" yyyy "will be on" dddd.')
Second, the ToString() method has many of overloads, one of which accepts any object implementing the IFormatProvider interface, which happens to include “CultureInfo” objects:
PS> $christmasEve.ToString OverloadDefinitions ------------------- string ToString() string ToString(string format) string ToString(System.IFormatProvider provider) string ToString(string format, System.IFormatProvider provider) string IFormattable.ToString(string format, System.IFormatProvider formatProvider) string IConvertible.ToString(System.IFormatProvider provider)
Here is a solution to output the days of the week in English, regardless of your own operating system language:
$christmasEve = Get-Date -Date '2018-12-24' $culture = [CultureInfo]'en-us' $christmasEve.ToString('"Christmas Eve in" yyyy "will be on" dddd.', $culture)
Christmas Eve in 2018 will be on Monday.
Start playing with other locales, and for example find out what “Monday” is in Chinese or Thai:
$christmasEve = Get-Date -Date '2018-12-24' $culture = [CultureInfo]'zh' $christmasEve.ToString('"Monday in Chinese: " dddd.', $culture) $culture = [CultureInfo]'th' $christmasEve.ToString('"Monday in Thai: " dddd.', $culture)
Monday in Chinese: 星期一. Monday in Thai: จันทร์.
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!