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?
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?
I think this will work:
$currentThread = [System.Threading.Thread]::CurrentThread
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$currentThread.CurrentCulture = $culture
$currentThread.CurrentUICulture = $culture
This idea came from:
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
}
}
For WinServer2012 and Win8 you can use Set-Culture
. As Set-Culture
sets the culture for your user, you'd have to open another powershell instance to benefit from that. Also it does not change the culture of running ps instances. Of course you could start up a new powershell instance in your current instance then. This is not exactly what is asked for, but closely related.
Would be neat, if they'd port that back to Win7 and Server 2008 or make it a feature of powershell itselft.
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.
[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.
See here for details of ObjectCmdletBase.Culture
Property.
Gets and sets the value of the Culture parameter of the derived cmdlet.
Set-Culture
works on local server but it does not work when you run it on remote computer
Invoke-Command -ComputerName $server -ScriptBlock{
#Requires -RunAsAdministrator
Set-Culture -CultureInfo en-GB;
Set-TimeZone -Id "GMT Standard Time"
}