setting culture in powershell script, sum

39 Views Asked by At

I'm loading some CSV data in a powershell script and doing a group and sum on a field. It as such work fine but the sum is not calculated correcty, as it uses wrong culture setting, for some reason. My data use , as separator and not .

I have tried to set the culture, but does not work.

Set-Culture -CultureInfo da-DK;

what can i Do?

2

There are 2 best solutions below

0
sirtao On

To set a Culture for only the scope of a script use [cultureinfo]::CurrentCulture=Get-Culture -Name da-DK (it starts having effect only for the line after it has been called)

But if you are importing from a CSV from a specific Culture then you should use Import-Csv with -UseCulture after setting the culture

(obviously da-DK would change depending on the culture involved)

0
Chris Ryan On

A sum would be a integer maths sum. When you import from a csv, it comes in as text. So that might be your problem. rather than the delimiter. (CSV defaults to ',' comma delimiter. but you can change it during the import. for example

import-csv -delimiter '.' file.csv

What it sounds like you need to do though, is change the column you want to a integer (number). As you do not paste your CSV format, here is a example

Name,Age

dave,21

steve,45

now import it, and convert the age column to a number:

$CSVData = import-csv myfile.csv | select Name,@{l='Age';e={[int]$_.Age}}

Now, the variable $CSVData will have the age column as a number, and this be sortable and able to do maths on etc.