Reputation: 451
Heys guys,
at first: i am pretty new to the mighty world of powershell - second: my problem is that i want to change the current culture to edit an excel workbook.
The situation that led me to ask a new question about this topic (yes, i know that there are a lot of similar topic about "changing current culture" and "how to edit an excel workbook using powershell", dont worry) is that many answers (like this) are a good point to start - but it won't work for me!
So here's my actual problem: I got a script that says
function Set-Culture ([System.Globalization.CultureInfo] $culture)
{
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
}
Get-Culture
Set-Culture('en-us')
Get-Culture
And a result that says
LCID Name DisplayName
---- ---- -----------
3079 de-AT German (Austria)
3079 de-AT German (Austria)
But (as you can see from above) wI ant to change the current culture to en-US. Where's my mistake?
Upvotes: 1
Views: 1655
Reputation: 201672
If you are on PowerShell v2, it is MTA which means IIRC you can get a different thread for every pipeline invocation. V1, V3 and V4 are STA however, I believe PowerShell resets the culture at certain points in execution. I believe something like the following will work better:
function Using-Culture
{
param(
[Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"),
[ScriptBlock]$script = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}")
)
$OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
$OldUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
try {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
Invoke-Command $script
}
finally {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture
}
}
Upvotes: 3