Score:1

How do I optionally specify parameters in Powershell?

cn flag

I'm porting some old code from AutoHotkey 'for laughs' and think I've stumbled across an unintended feature it had...

I'm wanting to be able to do something 'like'

$ws = "Minimized"
$parameters = "/k dir F:\" 
start-process cmd.exe ( $(if($parameters){"-argumentlist $parameters"}) )( $(if ($ws){"-windowystyle $ws"}) )

But it never 'concatenates' them into the one command 'holistically' without also throwing them all at cmd.exe - I'm naively hoping Powershell can 'catch' the WindowStyle itself, for example, and minimize the window (currently, it's passed straight to cmd.exe which ignores it).

I know I'm a bit mad, but I'm also struggling to find the right terms to search for - about_parsing wasn't helpful and the bazillion examples of concatenating strings is no good either - I'm really wanting Powershell to be loose enough to allow me to switch from strings to parameters dynamically, which I'm guessing goes against a conscious design decision somewhere...

So the alternative is a bunch of if statements to accommodate the various permutations of options...

if ($ws -and -(not $parameters)) {start-process cmd.exe -windowystyle $ws}
if ($parameters -and -(not $ws)) {start-process cmd.exe -argumentlist $parameters}
if ($parameters -and $ws) {start-process cmd.exe -argumentlist $parameters -windowystyle $ws}
...ad nauseum

Unless someone's got any better ideas?

Score:4
tz flag

Part of the problem with your "like" example is that it's grouping everything that comes after cmd.exe and passing it to Start-Process as a single-string second positional parameter which is ArgumentList. So you're effectively running this:

Start-Process -FilePath 'cmd.exe' -ArgumentList "-argumentlist /k dir F:\ -windowstyle Minimized"

Splatting might help you out a bit here. You can use a hashtable to dynamically build the set of parameters that get sent before you actually call Start-Process. For example:

# initialize a hashtable we'll use to splat with later
# that has all of the params that will always be used.
# (it can also be empty)
$startParams = @{
    FilePath = 'cmd.exe'
}

# conditionally add your WindowStyle
if ($ws) {
    $startParams.WindowStyle = $ws
}

# conditionally add your ArgumentList
if ($parameters) {
    $startParams.ArgumentList = $parameters
}

# run the function with the splatted hashtable
Start-Process @startParams
cn flag
You legend - thanks, I knew it was possible but just hadn't grasped what the fix was. Works beautifully. I figured out $startParams.Wait = $true works as well (for parameters that don't actually have a value, but are switches)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.