.net win7或win2008如何使用Powershell自定义“区域和语言”设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9736641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:18:57  来源:igfitidea点击:

How to use Powershell to customize "Region and Language" settings in win7 or win2008

.netwindows-7powershellcommand-linewindows-server-2008-r2

提问by Leo

I am new to Powershell and I have searched the internet for whole day but still cannot find out how to customize "Region and Language" settings using Powershell in win7 or win2008.

我是 Powershell 的新手,我已经在互联网上搜索了一整天,但仍然无法找到如何在 win7 或 win2008 中使用 Powershell 自定义“区域和语言”设置。

I want to change the following settings within Powershell:

我想在 Powershell 中更改以下设置:

  1. Current System Locale

  2. Short Date and Long Date format

  3. Short Time and Long Time format

  4. Current Location

  1. 当前系统区域设置

  2. 短日期和长日期格式

  3. 短时间和长时间格式

  4. 当前位置

Anybody knows how to do that using Powershell? Cmd/Bat/.NET solutions also welcome!

有人知道如何使用 Powershell 做到这一点吗?也欢迎 Cmd/Bat/.NET 解决方案!

回答by Marc Durdin

I know your question was about Windows 7; this information may be helpful for those now running newer versions. Set-WinUserLanguageList, New-WinUserLanguageListand Get-WinUserLanguageListin Windows 8 and up let you control the installed languages. For example to add a language:

我知道您的问题是关于 Windows 7;此信息可能对现在运行较新版本的用户有所帮助。Set-WinUserLanguageListNew-WinUserLanguageListGet-WinUserLanguageList在 Windows 8 及更高版本中让您控制已安装的语言。例如添加语言:

$list = Get-WinUserLanguageList
$list.Add("fr-FR")
Set-WinUserLanguageList $list

Set-Culturein PowerShell 3 lets you change culture, for example to choose defaults for Germany:

Set-Culture在 PowerShell 3 中,您可以更改文化,例如为德国选择默认值:

Set-Culture de-DE

Or, to set custom formats:

或者,设置自定义格式:

$culture = Get-Culture
$culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
$culture.DateTimeFormat.LongDatePattern = 'dddd, d MMMM yyyy'
$culture.DateTimeFormat.ShortTimePattern = 'h:mm tt'
$culture.DateTimeFormat.LongTimePattern = 'h:mm:ss tt'
Set-Culture $culture

To change the location, use Set-WinHomeLocation, for example to set the user's location to Austria:

要更改位置,Set-WinHomeLocation例如使用将用户的位置设置为奥地利:

Set-WinHomeLocation -GeoId 14

MSDN has a list of GeoIdsand a TechNet has a reference for international settings cmdlets.

MSDN 有一个GeoId 列表,TechNet 有一个国际设置 cmdlet的参考。

回答by rob

@bourne lead me to

@bourne 带我去

& $env:SystemRoot\System32\control.exe "intl.cpl,,/f:`"c:\setKeyboardUK.xml`""

Note the lack of a space between the ,, and /f, the use of quotes around the whole thing and the back tick to escape the quotes around the path (that are necessary).

请注意 , 和 /f 之间缺少空格,在整个事物周围使用引号,并使用反勾号来转义路径周围的引号(这是必要的)。

This is my setKeyboardUK.xml file

这是我的 setKeyboardUK.xml 文件

<gs:GlobalizationServices xmlns:gs="urn:longhornGlobalizationUnattend"> 
<!--User List-->
<gs:UserList>
    <gs:User UserID="Current" CopySettingsToDefaultUserAcct="true" CopySettingsToSystemAcct="true"/> 
</gs:UserList>
<gs:UserLocale> 
    <gs:Locale Name="en-GB" SetAsCurrent="true"/> 
</gs:UserLocale>
<!--location--> 
<gs:LocationPreferences> 
    <gs:GeoID Value="242"/> 
</gs:LocationPreferences>
<gs:InputPreferences>
    <!--en-GB--> 
    <gs:InputLanguageID Action="add" ID="0809:00000809" Default="true"/> 
</gs:InputPreferences>

To check if the settings are applied(because failures are silent) open Event Viewer and "Application and Services Logs" then "Microsoft", "Windows", "International","Operational" any successful changes or failures are logged here(the logging is enabled by default).

要检查是否应用了设置(因为失败是静默的),请打开事件查看器和“应用程序和服务日志”,然后在“Microsoft”、“Windows”、“国际”、“操作”中记录任何成功的更改或失败(日志记录默认启用)。

FYI I did all this on Powershell 3 on Windows 2008 R2 64bit. YMMV

仅供参考,我在 Windows 2008 R2 64 位上的 Powershell 3 上完成了所有这些。青年会

回答by bourne

Hey I know this is kind of old, but I had to do the same but I did it in a batch script. I am now currently trying to figure out how to do it in powershell.

嘿,我知道这有点旧,但我必须这样做,但我是在批处理脚本中完成的。我现在正试图弄清楚如何在 powershell 中做到这一点。

Check out my post here - https://superuser.com/questions/353752/windows-7-change-region-and-language-settings-using-a-script

在这里查看我的帖子 - https://superuser.com/questions/353752/windows-7-change-region-and-language-settings-using-a-script

The same should apply in Powershell using the following command:

使用以下命令同样适用于 Powershell:

PS C:\> & $env:SystemRoot\System32\control.exe "intl.cpl,, /f:path\to\xml\file\change_system_region_to_US.xml"

However, for me it doesn't work for some reason even though the command executes without error, the changes don't actually take effect.

但是,对我而言,即使命令执行没有错误,它也因某种原因不起作用,但更改实际上并未生效。

If you run the same command from a standard CMD window, the changes take effect immediately. And if you remote the quotations to match the way you would run it in a CMD window you get the following error:

如果您从标准 CMD 窗口运行相同的命令,更改将立即生效。如果您远程引用以匹配您在 CMD 窗口中运行它的方式,您会收到以下错误:

PS C:\> & $env:SystemRoot\System32\control.exe intl.cpl,, /f:"path\to\xml\file\change_system_region_to_US.xml"
    Missing argument in parameter list.
    At line:1 char:50
    + & $env:SystemRoot\System32\control.exe intl.cpl,, <<<<  /f:"path\to\xml\file\change_system_region_to_US.xml"
        + CategoryInfo          : InvalidOperation: (,:String) [], RuntimeException
        + FullyQualifiedErrorId : MissingArgument

Powershell doesn't appear to like the comma's very much.

Powershell 似乎不太喜欢逗号。

Doing so in a .bat file though works like a charm. Just make sure you get your country codes and stuff right. It might require a little tinkering to get the .xml file to change the parameters you want.

虽然在 .bat 文件中这样做很有吸引力。只要确保你的国家代码和东西是正确的。获取 .xml 文件以更改所需的参数可能需要稍加修改。