Working with a CSV file; I had a situation where the command line would give the output I wanted but the script would get the object information. I found I could get what I needed by converting the output to a script but it introduced white space. A computer name would turn into:
What was interesting was the system commands would treat the white space as part of the computer name which of course cause the commands to fail.
Look around the Net; I found a command to trim white space.
$server = $server.trim()
The system name returned to proper size and what is great about this command is that it will trim white space from both sides.