Powershell: Converting a String to an Integer

In creating a Powershell script to create a Microsoft Word document from CSV data I came across a little problem. In this snippet of code (the $patientAge and $patientName variables are populated with data from the CSV file) I want to highlight a patient’s name in yellow if they’re a minor:


If ($patientAge -lt 18)
{
$objSelection.font.Shading.BackgroundPatternColor = 65535
}
$objSelection.TypeText($patientName)

And it works fine if the patient age is two digits or less. But, if a patient age is 100 or higher – three digits, in other words – it would trigger as if they were a minor.

The fix was to convert the variable I’m using for the patient age into an integer and run the logic against it instead:


$patientAgeInt=[int]$patientAge
If ($patientAgeInt -lt 18)
{
$objSelection.font.Shading.BackgroundPatternColor = 65535
}
$objSelection.TypeText($patientName)

Boom, problem solved.