# How does PowerShell split unquoted command-line arguments?

Hi! I asked this on Stack Overflow last week, and some helpful people turned up but sadly without an answer, so I thought I’d try here.

I’m fairly new to PowerShell (more familiar with Bourne-style shells) and tripped over some odd (to me) command-line parsing behaviour: the . character sometimes starts a new argument, and sometimes doesn’t. Allow me to demonstrate:

PS C:\> function echo-args { foreach ($a in$args) { write-output \$a } }
PS C:\> echo-args a.txt .\b -c=d -e=.\f.xml -g.h -.i=j
a.txt
.\b
-c=d
-e=
.\f.xml
-g
.h
-.i=j
PS C:\>


I can more or less figure out when this happens, but I’d like to know why, and especially where to find documentation on the subject of how PowerShell separates command-line arguments.

I’ve looked in about_Parsing, about_Command_Syntax, and Bruce Payette’s PowerShell grammar Documentation, but somehow none of these say much about how argument lists are split. There must be documentation, surely?

Do these arguments/parameters actually come from a certain command? Do you have the need to process such arguments/parameters? Is there a real live case to use such weird looking arguments?

The real use case that prompted this question is parameters that take a file name as an argument, e.g. -output=path\to\file – and the powershell autocompletion likes to add a .\ to file names, and -output=.\file.txt doesn’t work without quotes.

OK, but the usual way to provide values for parameters in PowerShell would be like this -output path\to\file or this -output .\file.txt. … no equal sign needed. To separate the parameter name from the parameter value you use a space charachter or a colon - no equal sign. And to separate multiple values for one parameter you use a comma.

This isn’t a powershell cmdlet, it’s an external program. Of course I can always quote the arguments, but that’s not the point. The solution is obvious, but I’d like to know what’s going on…

Hmmm … ok … I cannot explain it, sorry … but since PowerShell 6 and up is Open Source you could take a look at the source code on Github. … or you could offer a bounty on StackOverflow. This might motivate more or actually the right people.

thanks for taking the time!

Interesting. I have found that if the answer is not on StackOverflow (an amazing site for answers), it is going to be hard to find and maybe you should have a look a the PowerShell source code as Olaf has suggested. I actually had a situation where I needed add missing functionality to PowerShell IOT core and was able to use the Source code to get that done. Might be worth a look.