Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set culture in PowerShell?

Is there any PowerShell equivalent for:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

? Or how to set the force all stringfications to obey a culture independently from the machine configurations?

like image 544
Jader Dias Avatar asked Nov 05 '10 10:11

Jader Dias


2 Answers

This is the function I use for testing string/formats in other cultures:

function Using-Culture (
  [System.Globalization.CultureInfo]
  $culture = (throw "USAGE: Using-Culture -Culture culture -Script {...}"),
  [ScriptBlock]
  $script = (throw "USAGE: Using-Culture -Culture culture -Script {...}"))
{
    $OldCulture = [Threading.Thread]::CurrentThread.CurrentCulture
    $OldUICulture = [Threading.Thread]::CurrentThread.CurrentUICulture    
    try {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $culture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $culture        
        Invoke-Command $script    
    }    
    finally {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture    
    }
}
like image 56
Keith Hill Avatar answered Oct 23 '22 08:10

Keith Hill


This answer deals with the current culture, which determines settings such as date format, currency, number formatting, collating sequence, ...; by contrast, the current UI culture, determines the UI language (menus, error messages, ...); All elements discussed below have UI-culture analogs (e.g., Get-UICulture vs. Get-Culture, $PSUICulture vs. $PSCulture EXCEPT Set-Culture, for which there is no analog.

Changing to a different culture:

In the .NET Framework v4.6 and higher, you can now assign to [cultureinfo]::CurrentCulture (previously, it was read-only[1]; the [cultureinfo] PS type accelerator was introduced in PSv3); e.g.:

[cultureinfo]::CurrentCulture = 'de-DE'

is equivalent to (which also works in v4.5 or lower, down to at least v2):

[System.Threading.Thread]::CurrentThread.CurrentCulture = 'de-DE'

CAVEAT: PowerShell uses the invariant culture in string-related contexts, irrespective of what the current culture is - see this answer of mine.

  • Both methods change the culture for the current PowerShell instance (thread) only.

    • Caveat [fixed in PowerShell Core as of at least v6.0.2]: As has been noted, in order to try this in an interactive PowerShell session, enter all commands on a single line, because the culture-changing effect is limited to a single command line (this still applies on PSv3+, even though consoles there run in STA mode by default); e.g., to print a German date:
      [cultureinfo]::CurrentCulture = 'de-DE'; Get-Date # must be on same line
  • For a persistent culture change for the current user, use the Set-Culture cmdlet, but, as noted in mbx's helpful answer, this change only takes effect for future PowerShell instances, NOT the current one.


Querying culture settings:

  • [cultureinfo]::CurrentCulture and [System.Threading.Thread]::CurrentThread.CurrentCulture reflect the current PowerShell instance's effective culture.

  • By contrast, the Get-Culture cmdlet (PSv3+) and the automatic $PSCulture variable (PSv3+; read-only) invariably reflect the current PowerShell instance's culture at startup time; i.e., they always reflect the current user's persistently configured culture at the time the current PowerShell instance was started (irrespective of instance-only changes via [cultureinfo]::CurrentCulture = ... or future persistent changes via Set-Culture performed in that instance).


[1] See the docs; to determine whether you have at least v4.6 installed, look for the Version: value in the output from Get-Item 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'.
Note that the framework version is distinct from the CLR (runtime) version as reported by $PSVersionTable.CLRVersion; for instance, the v4.6 framework is based on the v4.0 CLR - see the docs.

like image 21
mklement0 Avatar answered Oct 23 '22 08:10

mklement0