String Methods do not Exist Error

I have a PowerShell script that I wrote on an Windows 7 workstation that has English as the OS language. Script works without issue. If I run the same script on a Windows 7 workstation that has French as the OS language, I receive non-terminating errors when the script runs and tries to call the Trim method of the string class. The error I receive (translated to English) is “Calling the method failed because [System.String ] does not contain a method called ‘Trim’”

The English workstation has PS 4.0 installed. The French workstation has PS 2.0 installed.


You mean beside upgrade the French workstation to 4.0? Is there some compelling reason why it remains at 2.0?

The error message has your hint. :slight_smile: Type is [System.String ] (an array of strings). Arrays don’t have a Trim() method.

Starting with PSv3, PowerShell gave us the ability to access properties and methods on arrays as though they were a single object. $arrayOfStrings.Trim(), etc. If you need your code to be friendly to PowerShell v2, then you could do this instead: $arrayOfStrings | ForEach-Object { $_.Trim() }

BTW, Bob, please email me regarding this year’s PowerShell Heroes. . Haven’t been able to find contact information for you anywhere.

Bob, I’ve been wanting my organization to update to PS v4 for some time now. It was ready to go for deployment but they didn’t like the fact that it wasn’t as easily removed since v4 requires registry backups prior to uninstall.

Much appreciated Dave. I hadn’t realized that change since I’ve only every really worked on version 4 when I started to learn PowerShell.