I am working on a PowerShell wrapper script to run multiple commands on multiple machines. The first thing I wanted to do was to chop up a space or comma delimited argument. (If you make an argument mandatory, and the user does not provide it, PowerShell will prompt you for it – and when prompted you can use space delimited values).
I was going to use the split() function, but I did not know how to handle “multiple spaces”. For example:
("123 4 5678").Split() = 123 4 5678"
I would end up with “empty” array members. I found this post, and they guy used “| where-object {$_ -ne ”“}” to filter out the empty values. But then I stumbled upon a better way to do it, using the built in “StringSplitOptions” of RemoveEmptyEntries.
("123 4 5678").split(" ",[StringSplitOptions]'RemoveEmptyEntries')
would result in:
123 4 5678
If you don’t know if you are going to have space or comma delimited then I used:
$Argument.split(" ",[StringSplitOptions]'RemoveEmptyEntries') | foreach { $_.split(",",[StringSplitOptions]'RemoveEmptyEntries')}