Without reading your code verbatim I would have to guess that you have a leading space/line break/hidden char in the string.
Fortunately though your situation is one that lends it self to powershell. mainly because you really don't need a "script" at all.
(" abcd efg h i j ").split(" ") | where-object {$_ -ne " "} On a side note your post led me down a
rabbit hole as to why it all worked. ... so thanks!
~Glenn