Trouble with quotes in PowerShell

Nancy J 41 Reputation points
2022-05-20T12:39:58.09+00:00

I have a PowerShell script that starts a process to open our FTP client and download some files. It works great on it's own but I now need to pass a variable to it but the result of that variable needs to be in quotes and I'm struggling with how best to do that.

Here is what I use now that works very well:

Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/ -d local:C:\Test\"

What I need to do is change the item in quotes after -ArgumentList to include a variable. I can't figure out how to set this up so that variable is in quotes when it's in the Start-process line:

$PDate = Get-Date -Format yyyyMMdd
$arglist = "-s TESTCONN:/" + $PDate + "* -d local:C:\Test\"

Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList $arglist

I need the final command to look like this (assuming date of 2022-05-20):

Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/20220520* -d local:C:\Test\"

I've tried double quotes, single quotes and multiple combinations of the two but I cannot get it to output correctly.

What am I missing?

Thanks in advance.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Newbie Jones 1,306 Reputation points
    2022-05-20T15:25:39.263+00:00

    Backticks to encapsulate the double quotes.

    $path = "`"C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe`""
    $PDate = Get-Date -Format yyyyMMdd
    $parameter = "`"-s TESTCONN:/$Pdate* -d local:C:\Test\`""
    Write-Host Start-Process $path -ArgumentList $parameter
    

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-05-20T14:58:50.063+00:00

    That doesn't seem like it's very difficult to achieve. Is this what you're after?

    $arglist = "-s TESTCONN:/$PDate* -d local:C:\Test\"
    

    There's no need to place quotes around the $arglist when it's used as the value for the -ArgList parameter.

    0 comments No comments

  2. Nancy J 41 Reputation points
    2022-05-20T15:17:00.777+00:00

    @Rich Matheisen - That's neat, I didn't know you could combine like that. Thanks for the tip.

    What I run into though with that option also is that the end result is:

    Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList -s TESTCONN:/20220520* -d local:C:\Test\  
    

    I need everything after -ArgumentList to be inside quotes like this:

    Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/20220520* -d local:C:\Test\"  
    

  3. Nancy J 41 Reputation points
    2022-05-20T15:26:32.983+00:00

    I understand how to put the variable in the command but it doesn't run if I do, it just exits with: Start-Process : A positional parameter cannot be found that accepts argument 'TESTCONN:/'20220520*

    I need to have the variable include the quotes so the end result is correct. Sorry if I wasn't clear but that's where I'm stuck.

    0 comments No comments