Please feel free to provide feedback or file bugs here.

[Bug] Command line parsing: spaces and \"

This is my powershell version
==========
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.15063.674
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.674
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
=====END=====

Let's see a use scene
========== WindowsPowershell
function FormatReg([string]$value)
{
$value = $value -replace '"', '\"'
$value = '"' + $value + '"'
return $value
}
$temp = FormatReg '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy) -ne ''AllSigned'') { Set-ExecutionPolicy -Scope Process Bypass }; & ''%1''"'
reg.exe add HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command /f /d $temp
=====END=====
> Error: Invalid syntax.
> Type register Add/? "Learn about usage information.

========== cmd
reg.exe add HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command /f /d "\"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; ^& '%1'\""
=====END=====
> Error: Access denied (if you not run as administrator)
or
> Operation successful (Warning, this will modify your registry)
comment: ^ is cmd escape characters. So enter ^& instead of &

Why???
After I debug, it was found that \" and space caused (\" is double quotation escape characters)
Let's use this simplified string: "\"a b c\""

First, build a command parameter viewer. ConsoleTest.exe
========== C# Program.cs
using System;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
for (var i = 0; i < args.Length; i++)
{
Console.WriteLine($"{i}: {args[i]}");
}
Console.ReadLine();
}
}
}
=====END=====

As this execute
========== WindowsPowershell
.\ConsoleTest.exe '"\"a b c\""'
=====END=====
> 0: "a
> 1: b
> 2: c"

========== WindowsPowershell
Start-Process -FilePath .\ConsoleTest.exe -ArgumentList '"\"a b c\""'
Start-Process -FilePath .\ConsoleTest.exe -NoNewWindow -ArgumentList '"\"a b c\""'
=====END=====
> 0: "a b c"
> 0: "a b c"

========== cmd
.\ConsoleTest.exe "\"a b c\""
=====END=====
> 0: "a b c"

========== Powershell Core
.\ConsoleTest.exe '"\"a b c\""'
=====END=====
> 0: "a b c"

See, when windows powerhell is executed directly, parsing exceptions occur.
When exist \", the space will be wrong to cut off the parameters.

2 votes
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    zskycat shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • zskycat commented  ·   ·  Flag as inappropriate

        sorry, my English is bad, and the space indent is automatically deleted by the editor.

      Feedback and Knowledge Base