title: How do I use PowerShell to split a DOS-style command line into its parameters
body: <p>I have a Powershell script that (as one of its options) reads a user-defined pre-execution command from a file and needs to execute it.
  The user-defined pre-execution command is expected to be an ordinary DOS-style command.
  I can split the command by spaces and then feed it to the PowerShell "&amp;" to get it executed:</p>
  
  <pre><code>$preExecutionCommand = "dir D:\Test"
  $preExecutionArgs = $preExecutionCommand -split '\s+'
  $preExecutionCmd = $preExecutionArgs[0]
  $preExecutionNumArgs = $preExecutionArgs.Length - 1
  if ($preExecutionNumArgs -gt 0) {
    $preExecutionArgs = $preExecutionArgs[1..$preExecutionNumArgs]
    &amp; $preExecutionCmd $preExecutionArgs
  } else {
    &amp; $preExecutionCmd
  }
  </code></pre>
  
  <p>But if the user-defined command string has spaces that need to go in the arguments, or the path to the command has spaces, then I need to be much smarter at parsing the user-defined string.</p>
  
  <p>To the naked eye it is obvious that the following string has a command at the front followed by 2 parameters:</p>
  
  <pre><code>"C:\Program Files\Tool\program1" 25 "the quick brown fox"
  </code></pre>
  
  <p>Has anyone already got a function that will parse strings like this and give back an array or list of the DOS-style command and each of the parameters?</p>