Skip to content

powershell.exe -Command parameter should handle quotes consistently across hosts and platforms #1583

Closed

Description

@vors

Steps to reproduce

powershell -Noprofile -Command '"[Console]::Error.Write(""""foo"""")"'

Expected behavior

This is inbox windows v5.1 behavior

PS C:\> $powershell = Join-Path -Path $PsHome -ChildPath "powershell"
PS C:\> & $powershell -Noprofile -Command '"[Console]::Error.Write(""""foo"""")"'
fooPS C:\>

Actual behavior

"where are quotes around foo"?

PS /Users/vors/dev/PowerShell> $powershell = Join-Path -Path $PsHome -ChildPath "powershell"                                                 
PS /Users/vors/dev/PowerShell> & $powershell -Noprofile -Command '"[Console]::Error.Write(""""foo"""")"'                                     
At line:1 char:24                                                                                                                            
+ [Console]::Error.Write(foo)                                                                                                                
+                        ~                                                                                                                   Missing ')' in method call.                                                                                                                 
 At line:1 char:24                                                                                                                            + [Console]::Error.Write(foo)                                                                                                                
+                        ~~~
Unexpected token 'foo' in expression or statement.
At line:1 char:27
+ [Console]::Error.Write(foo)
+                           ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

Environment data

> $PSVersionTable
Name                           Value                                                                                                        
----                           -----                                                                                                        
PSVersion                      5.1.10032.0                                                                                                  
PSEdition                      PowerShellCore                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                      
BuildVersion                   3.0.0.0                                                                                                      
GitCommitId                    v6.0.0-alpha.7                                                                                               
CLRVersion                                                                                                                                  
WSManStackVersion              3.0                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                          
SerializationVersion           1.1.0.1

Activity

  1. changed the title [-]Command parameter should handle quotes consistently across hosts[/-] [+]powershell.exe -Command parameter should handle quotes consistently across hosts and platforms[/+] on Jul 31, 2016
  2. modified the milestone: on Aug 4, 2016
  3. TSlivede commented on Oct 19, 2016

    @TSlivede

    Well I think, this is just a consequence of #1995

    If you launch powershell from bash (on linux):
    powershell -Noprofile -Command '[Console]::Error.Write("foo")'
    powershell gets [Console]::Error.Write("foo") as argv[3] and it works.

    If you launch powershell from cmd:
    powershell -Noprofile -Command "[Console]::Error.Write(\"foo\")"
    powershell gets [Console]::Error.Write("foo") as argv[3] ** and it works.

    When #1995 is fixed for window and linux
    powershell -Noprofile -Command '[Console]::Error.Write("foo")'
    will work on window and linux from within powershell.

    Therefore I think this bug can be closed, as powershell already handles arguments the same on windows and linux: It takes all arguments from argv that follow after -Command, puts spaces in between and executes the command.

    ** (after commandline is splitted via CommandLineToArgvW or [System.Environment]::GetCommandLineArgs() or whatever powershell uses...)

  4. joeyaiello commented on May 23, 2017

    @joeyaiello
    Contributor

    This is my current repro (still different from 5.1, but at least with the quotes still there):

    C:\temp> $powershell = Join-Path -Path $PsHome -ChildPath "powershell"
    C:\temp> & $powershell -Noprofile -Command '"[Console]::Error.Write(""""foo"""")"'
    At line:1 char:26
    + [Console]::Error.Write(""foo"")
    +                          ~
    Missing ')' in method call.
    At line:1 char:26
    + [Console]::Error.Write(""foo"")
    +                          ~~~~~
    Unexpected token 'foo""' in expression or statement.
    At line:1 char:31
    + [Console]::Error.Write(""foo"")
    +                               ~
    Unexpected token ')' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
    
  5. TSlivede commented on May 23, 2017

    @TSlivede

    I hope that this will soon be simplified, but the correct usage is currently:

    & $powershell -Noprofile -Command '[Console]::Error.Write(\"foo\")'
    

    (If the RFC gets accepted, the backslashes are no longer necessary and must be removed.)

    If you want to be safe, you could use --%

    & $powershell --% -Noprofile -Command "[Console]::Error.Write(\"foo\")"
    
  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions