I am currently importing a CSV file which has a column that is all numbers. I am attempting to cast it as an int and only pull ones that are greater than 100. I have manually gone through this CSV file, and I can confirm that there are three rows with a greater-than-100% value. However, this always returns 0. What am I doing wrong?
$percentTooLarge = Import-Csv path\file.csv | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [int]; $_ } | Where-Object { $_.SumHoldingPercent -gt 100 } | Measure-Object
$numPercentTooLarge = $percentTooLarge.Count
Because of the way compare operators work in PowerShell, this should do the trick:
Basically, PowerShell, when you compare things, tries to convert right to the type of left. If you put a value from ipcsv first – left will be a string. If you put a numeric first – it will convert the value from the CSV file to a number (it will be smart enough to keep the type big-enough ;))
I tested with this code:
… and the results seems OK.