question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

-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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Dec 6, 2017

@PowerShell/powershell-committee reviewed this and agree that the change in behavior makes sense and has low risk

1reaction
mklement0commented, Aug 8, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Start-Process cmdlet when ArgumentList is ...
I am using Powershell 7 and do not have this problem, I run Start-Process -ArgumentList $args in a function where $args is sometimes...
Read more >
Start-Process (Microsoft.PowerShell.Management)
Specifies parameters or parameter values to use when this cmdlet starts the process. Arguments can be accepted as a single string with the...
Read more >
Pass quoted argument string to Start-Process in PowerShell
The problem with the Start-Process cmdlet in PowerShell is that it uses a string array for arguments, so the path is broken up...
Read more >
Groovy Language Documentation
The spread operator is null-safe, meaning that if an element of the collection is null, it will return null instead of throwing a...
Read more >
The java Command
A class descriptor is in decorated format ( Lname; ) when it should not be. A NULL parameter is allowed, but its use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found