在PowerShell中切换区域和语言格式

时间:2016-09-21 07:52:26

标签: powershell culture

我有一个powershell脚本,它操作excel(.xlsx)文件,然后从.csv文件中复制数据。但是,如果我将我的区域和语言格式更改为美国(我在挪威),脚本将只能正常运行。我的问题是,我如何在powershell脚本中执行此操作。我希望脚本可以被办公室中的其他人使用,而无需更改其区域设置。

我试过了:

id

但这不会改变区域设置,如下图所示:

1 个答案:

答案 0 :(得分:0)

您可以在powershell中使用国际模块,我们有一些cmdlet可以满足要求。

Internation Module Usage

Internation module Script

<强>替代: 使用以下功能进行设置:

Function Set-RegionSettings
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [String]$Country,
        [Parameter(Mandatory=$true)]
        [String]$ShortDate,
        [Parameter(Mandatory=$true)]
        [String]$LongDate,
        [Parameter(Mandatory=$true)]
        [String]$ShortTime,
        [Parameter(Mandatory=$true)]
        [String]$TimeFormat,
        [Parameter(Mandatory=$true)]
        [String]$FirstDayOfWeek
    )

    $RegKeyPath = "HKCU:\Control Panel\International"
    If ($Country)
    {
        Set-ItemProperty -Path $RegKeyPath -Name sCountry -Value "$Country"
        Write-Verbose "Successfully changed value of country."
    }

    If ($ShortDate)
    {
        Set-ItemProperty -Path $RegKeyPath -Name sShortDate -Value "$ShortDate"
        Write-Verbose "Successfully changed value of short date."
    }

    If($LongDate)
    {
        Set-ItemProperty -Path $RegKeyPath -Name sLongDate -Value "$LongDate"
        Write-Verbose "Successfully changed value of long date."
    }

    If($ShortTime)
    {
        Set-ItemProperty -Path $RegKeyPath -Name sShortTime -Value "$ShortTime"
        Write-Verbose "Successfully changed value of short time."
    }

    If($TimeFormat)
    {
        Set-ItemProperty -Path $RegKeyPath -Name sTimeFormat -Value "$TimeFormat"
        Write-Verbose "Successfully changed value of time format."
    }

    If($FirstDayOfWeek)
    {
        Set-ItemProperty -Path $RegKeyPath -Name iFirstDayOfWeek -Value "$FirstDayOfWeek"
        Write-Verbose "Successfully changed value of first day of week."
    }

    $sCountry = (Get-ItemProperty -Path $RegKeyPath -Name sCountry).sCountry
    $sShortDate = (Get-ItemProperty -Path $RegKeyPath -Name sShortDate).sShortDate
    $sLongDate = (Get-ItemProperty -Path $RegKeyPath -Name sLongDate).sLongDate
    $sShortTime = (Get-ItemProperty -Path $RegKeyPath -Name sShortTime).sShortTime
    $sTimeFormat = (Get-ItemProperty -Path $RegKeyPath -Name sTimeFormat).sTimeFormat
    $iFirstDayOfWeek = (Get-ItemProperty -Path $RegKeyPath -Name iFirstDayOfWeek).iFirstDayOfWeek

    $Obj = New-Object -TypeName PSObject -Property @{
    "Country" = $sCountry
    "Short date" = $sShortDate
    "Long date" = $sLongDate
    "Short time" = $sShortTime
    "Long time" = $sTimeFormat
    "First day of week" = $iFirstDayOfWeek
    }

    Write-Host "The current date and time formats:"
    $Obj
}

<强> USAGE:

Set-RegionSettings -ShortDate "M/d/yyyy" -LongDate "dddd,MMMM d,yyyy" -ShortTime "h:mm tt" -TimeFormat "h:mm:ss tt" -FirstDayOfWeek "Sunday" -Country "United States"

希望它有所帮助。