Here is another strategy to make sure a text does not exceed a given length. In contrast to our previous tip, this code will not pad spaces in case the text is shorter than the maximum length:
$text = 'this' $MaxLength = 10 $CutOff = [Math]::Min($MaxLength, $text.Length) $text.Substring(0,$CutOff)
Key is the Min() method which determines the smaller of two values.