Calling a program with mutliple parameters in variables

Hi,
It have tried to call a program from within a script, with parameters stored in variables, but nothing works. For testing and explanation, I made a very simple script:

Get-ChildItem d:\test -recurse 2>>err.txt
$prog = "Get-ChildItem"
$param1 = "d:\test"
$param2 = "-recurse"
$param3 = "2>>d:\err.txt"
Write-Host $prog $param1 $param2 $param3
& $prog $param1 $param2 $param3

Line 1 works and Line 6 produces an output identical to line 1. However, line 7 does not produce the desired result:

Get-ChildItem : Es wurde kein Positionsparameter gefunden, der das Argument "2>>d:\err.txt" akzeptiert.
In D:\myFiles\Winni\Documents\Start\test.ps1:7 Zeichen:1
+ & $prog $param1 $param2 $param3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Other tries:

  • if I omit $param3, I do not get any output at all.
  • if I omit both $param2 and $param3, i.e. with a single parameter, it works correctly.
  • if I put all parameters in a single string, it considers the complete string as the path and tells me “cannot find the path”.

So the question is: how can I call from the script a program with multiple parameters, where these parameters shall be stored in variables??

Any ideas for me? Thanks!

Get-Childitem is a cmdlet, not a program. There is no reason to use the invocation operator. This is very different than your last post calling an external program. With cmdlets, it’s best to splat the parameters. First create a hashtable with the parameters and arguments

$params = @{
    Path = 'D:\test'
    Recurse = $true
}

Get-Childitem @params

The redirection of stderr to error log is NOT a parameter/argument for cmdlets or external programs so shouldn’t be considered as such. I think it might be best to hear what you’re actually trying to do as your example today doesn’t make sense.

1 Like

Sorry my posting made no sense - I am an absolute powershell beginner.

What I want is a script to run a program with several parameters, to redirect the error output to a file; and the parameters / error filename shall be stored beforehand in variables. It works if I enter the command directly in the Powershell:

& "c:\program files\notepad++\notepad++.exe" readme.txt -x4 -c5 2>>d:\err.txt

and it will work in a standard batch(cmd)-file with variables

set prog="c:\program files\Notepad++\notepad++.exe"
set p1=readme.txt
set p2=-n4
set p3=-c5
set p4=2>>err.txt

%prog% %p1% %p2% %p3% %p4%

but I do not manage to create a command like that in a PowerShell script. I tried several possibilities, the most obvious for me would be to do it exactly as in the batch file:

$prog="c:\program files\Notepad++\notepad++.exe"
$p1="readme.txt"
$p2="-n4"
$p3="-c5"
$p4="2>>d:\err.txt"
& $prog $p1 $p2 $p3 $p4

but this produces an error

"Cannot open file D:\2>>err.txt"
& $prog $p1 $p2 $p3 2>>$p4

where $p4 is just “d:\err.txt” instead

Does that work?

I think the thing that is producing the error is Notepad++; I don’t think that’s the actual error message though. i think it’s actually Cannot Open File D:\2>>D:\err.txt based on my reproduction of the issue.

I don’t use legacy apps much like this via cmd line these days but it seems to be it’s not properly referencing the path when you include the error stream redirect/append to the file.

If interested in PS, you may want to give Start-Process a go?

$prog="c:\program files\Notepad++\notepad++.exe"
$p1="readme.txt"
$p2="-n4"
$p3="-c5"
"d:\err.txt"
Start-Process -FilePath $prog -ArgumentList "$P1 $P2 $P3" -RedirectStandardError $p4

Note that you could just use ‘NotePad++’ for the filepath but that doesn’t seem to work with the redirect as outlined here: runtime error - PowerShell Start-Process -redirectStandardOutput throws: The system cannot find the file specified - Stack Overflow However, the redirect doesn’t appear to append errors so that may be a deal breaker, at least according to Start-Process: Provide the ability to append when redirecting stdout/stderr streams to file · Issue #15031 · PowerShell/PowerShell · GitHub. Still may be worth trying out though.

1 Like

The call operator doesn’t like the redirect, and Invoke-Expression chokes on the space in the path. However, a combination of the two works for me:

Invoke-Expression "& '$prog' $p1 $p2 $p3 $p4"

Thank you both for replies, both proposed solutions appear to work.
@matt-bloomfield : Interesting, but also very confusing…
@dotnVo : This appears more logical, and I will use the call via Start-Process.
So I am happy now, but I would be even happier if I had understood why what I tried did not work (i.e. why entering a command directly works, but using variables not). Anyway, it is probably not worth investigating further, now that I have a solution.