1

I have a powershell script which manipulates an excel (.xlsx) file and then copies in data from a .csv file. However, the script will only run properly if I change my region and language format to United States (I am in Norway). My question is, how do I do this in the powershell script. I want the script to be useable by others in the office, without having to change their region settings.

I have tried:

$RegKeyPath = "HKCU:\Control Panel\International"
Set-ItemProperty -Path $RegKeyPath -Name sCountry -Value "en-US"

But this does not change the region setting as shown in the picture below:

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ericvb86
  • 91
  • 1
  • 5
  • 2
    What's the actual problem with Excel processing? Locales change things like list and decimal separators. These can be overcome on other ways, too. – vonPryz Sep 21 '16 at 07:55
  • 2
    Issues with locale in Excel can usually be overcome by setting en-US culture for the calling thread, no need to change the system-wide regional settings – Mathias R. Jessen Sep 21 '16 at 08:45
  • I am calling the powershell script from batch: Powershell.exe -executionpolicy remotesigned -myscript.ps1 %var1% %var2% If I call it with my region format set to US, everything runs as expected. If my region settings are set to Norway, the following error repeatedly pops up from powershell: "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))" – Ericvb86 Sep 21 '16 at 09:23
  • 2
    Try [changing the current culture](http://stackoverflow.com/a/7052955) within Powershell. – vonPryz Sep 21 '16 at 09:33
  • This did the trick as far as I can tell. I'm having a few others run the script as well to make sure. Thanks! – Ericvb86 Sep 21 '16 at 10:29

1 Answers1

0

You can use the international module in powershell where we have certain cmdlets to fulfill the requirement.

Internation Module Usage

Internation module Script

Alternative: Use the below function to set it:

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"

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45