Did you get the results you expected? If your answer is “No” there seem to be a mistake in your expectations of the results you get from the code you used. To determine the exact output of the code you have to make it visible. Most of the time you can see additional charachters when you output a variable like this “‘$($Variable)’” (pay attention to the double and single quotes) … to compare your results you could use a custom object like this:
… as you can see - most of the objects or properties have a length property anyway … you should be able to use that if you need. You should try to write your code as least complex as possible to provide the result you desire.
Out-string adds a newline to each name, so that where clause will be true for every name. Trimming it will remove the newline and will be at least one less in length. Out-string is unnecessary in this case. Name is already a string. Out-string is almost never useful.