Skip to content

Start-Process -ArgumentList should accept an empty array / the null collection and interpret that as "no arguments to pass" #4520

Closed

Description

-ArgumentList (-Args) currently requires that a non-empty array be passed, which is inconvenient in scenarios where you want to pass arguments through, whether any were specified or not, such as via $Args.

An empty array or a null collection ([System.Management.Automation.Internal.AutomationNull]::Value) should be accepted and should be interpreted as "no arguments to pass".

Currently, passing $Args through in a function is cumbersome; imagine a function wpsh whose purpose is to invoke a Windows PowerShell window from PowerShell Core, with any arguments passed through:

function wpsh {
  # Pass all arguments through. 
  # To do this in a single Start-Process call, you need a cumbersome workaround
  # via an aux. hashtable and splatting.
  # Ideally,  
  #    Start-Process (Get-Command powershell.exe).Path -Args $Args 
  # should work directly.
  $htArgs  = if ($Args.Count) { @{ Args = $Args } } else { @{} }
  Start-Process (Get-Command powershell.exe).Path @htArgs
}

Steps to reproduce

# Or: [System.Management.Automation.Internal.AutomationNull]::Value, such as produced by `& {}`
$arguments = @()  
Start-Process code -Args $arguments

Expected behavior

code (the Visual Studio CLI, if installed) should open / activate, with no arguments being passed.

Actual behavior

Invocation fails with the following error message:

Start-Process : Cannot validate argument on parameter 'ArgumentList'.
The argument is null, empty, or an element of the argument collection contains a null value.
Supply a  collection that does not contain any null values and then try the command again.

Environment data

PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. changed the title [-]Start-Process -ArgumentList should accept empty arrays / the null collection and interpret that as "no arguments to pass"[/-] [+]Start-Process -ArgumentList should accept an empty array / the null collection and interpret that as "no arguments to pass"[/+] on Aug 7, 2017
  2. added
    Issue-Discussion
    on Aug 8, 2017
  3. iSazonov commented on Aug 8, 2017

    @iSazonov
    Collaborator

    Is the main motivation a dynamic construction of the parameter list? Any samples?

  4. mklement0 commented on Aug 8, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov): It is primarily about passing $Args through, though allowing empty arrays can also help with dynamically constructed parameter lists; I've added an example to the initial post, based on this real-world code.

  5. ZSkycat commented on Nov 29, 2017

    @ZSkycat

    I also encountered this problem.
    my code

    function RunAsAdministrator {
        $principal = [System.Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()
        $isAdministrator = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
        if (!$isAdministrator) {
            $cmd = [System.Environment]::GetCommandLineArgs()
            Start-Process -Verb 'runas' ($cmd[0] -replace '.dll$', '.exe') $cmd[1..$cmd.Length]
            exit
        }
    }
    RunAsAdministrator

    can work in Windows Powershell
    can not work in Powershell Core

    Ilya (@iSazonov)

  6. iSazonov commented on Nov 29, 2017

    @iSazonov
    Collaborator

    ZSkycat (@ZSkycat) It seem is another issue. Please open Issue-Question.

  7. iSazonov commented on Nov 29, 2017

    @iSazonov
    Collaborator
  8. mklement0 commented on Nov 29, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    Thanks.

    Re ZSkycat (@ZSkycat)'s issue: it is the same issue (and Windows PowerShell and PowerShell Core are therefore equally affected): if $cmd[1..$cmd.Length] evaluates to $null - i.e., if the PowerShell instance was originally invoked without any arguments - binding to -ArgumentList breaks.

  9. ZSkycat commented on Nov 29, 2017

    @ZSkycat

    Ilya (@iSazonov)
    This is the same issue. As Michael Klement (@mklement0) said

    Windows PowerShell support -ArgumentList equal $null

    Windows PowerShell And PowerShell Core will throw an error

    Start-Process : Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the
     argument collection contains a null value. Supply a collection that does not contain any null values and then try the
    command again.
    
  10. mklement0 commented on Nov 29, 2017

    @mklement0
    ContributorAuthor

    ZSkycat (@ZSkycat):

    Yes, it is the same issue, but Windows PowerShell also doesn't support an empty array / $null -ArgumentList value: try Start-Process notepad -ArgumentList $null

    As an aside: Unfortunately, the way Start-Process currently works may require additional, embedded quoting to pass arguments through.
    For instance, your function would fail if the session had been invoked with something like powershell -noexit -file "C:\Users\jdoe\t 1.ps1", due to the space in the filename - see #5576

  11. ZSkycat commented on Nov 29, 2017

    @ZSkycat

    Michael Klement (@mklement0)
    I try it, you are right.

  12. added
    Review - Committee
    Breaking-Change
    Committee-Reviewed
    on Nov 30, 2017
  13. 3 remaining items

  14. SteveL-MSFT commented on Mar 17, 2018

    @SteveL-MSFT
    Member

    Bruce Payette (@BrucePay) do you want to submit a PR for this?

  15. removed
    Breaking-Change
    on Mar 17, 2018
  16. added a commit that references this issue on Jun 22, 2018
  17. added a commit that references this issue on Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Committee-ReviewedIssue-DiscussionResolution-FixedWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions