0

I have a cmd file which calls a powershell script which displays the input.

The input to the cmd is a list of filenames which it forwards it to the powershellscript which accepts a string array.

When trying it out, the whole list of filenames goes as one parameter.

I tried the answers at the link here and here but no luck.

Below is the output when I run the cmd.

C:\Users\User1>C:\Sample.cmd "C:\file1.txt C:\file2.txt"
Processing file - C:\file1.txt C:\file2.txt

Unfortunately the input to the cmd(list of files) are received from an external program that invokes it.

The powershell script goes like this:

param
(
    [Parameter(Position = 0, Mandatory = $true)]
    [string[]] $sourceFiles
)

Function Sample_function
{
Param
    (
        [Parameter(Position = 0, Mandatory = $true)]
        [string[]] $sourceFiles
    )
    
    foreach($file in $sourceFiles)
    {
        Write-Host "Processing file - $file"        
        
    }
}

Sample_function $sourceFiles

And the cmd goes like this:

@echo off

set PS_File="C:\Sample.ps1"

powershell -FILE "%PS_File%" %*
joanis
  • 10,635
  • 14
  • 30
  • 40
  • _When trying it out, the whole list of filenames goes as one parameter_ I suggest you simply write some code in Powershell that splits that one parameter into an array, something like `.Split(" ")` – Nick.Mc Mar 14 '22 at 11:54
  • This is a quick solution but I am trying to understand if there is an out of the box solution. This looks like a pretty much used use case in many circumstances. – Sandeep Bhat Mar 14 '22 at 11:57

1 Answers1

2

In order to make an array parameter work with %*, use the ValueFromRemainingArguments setting:

param
(
    [Parameter(Position = 0, Mandatory = $true, ValueFromRemainingArguments = $true)]
    [string[]] $sourceFiles
)

Now PowerShell will correctly bind all expanded argument values to $sourceFiles even though they're separated by space and not ,

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I have already tried this like I mentioned in my question(the links), unfortunately I still get the same output. – Sandeep Bhat Mar 14 '22 at 12:03
  • @SandeepBhat It should work with `C:\Sample.cmd C:\file1.txt C:\file2.txt` (or `C:\Sample.cmd "C:\file1.txt" "C:\file2.txt"`) – Mathias R. Jessen Mar 14 '22 at 12:05
  • This will of course work, but like I said, the input to the cmd is form an external program unfortunately and it always sends a list of files separated with a space :( – Sandeep Bhat Mar 14 '22 at 12:07
  • @SandeepBhat Yeah but it doesn't add the quotes, does it? A space-separated list of file paths will work just fine with `ValueFromRemainingArguments` – Mathias R. Jessen Mar 14 '22 at 12:08
  • So basically the external program invokes the Sample.cmd with the following parameters: cmd.exe /C C:\Sample.cmd "C:\file1.txt" "C:\file1.txt" But when the cmd sends the parameters to powershell they go like "C:\file1.txt C:\file1.txt" – Sandeep Bhat Mar 14 '22 at 12:12
  • 1
    I'm entirely unable to reproduce that behavior. Launching `cmd /c sample.cmd C:\file1.txt C:\file2.txt` populates `$sourceFiles` correctly when `%*` and `ValueFromRemainingArguments` is used – Mathias R. Jessen Mar 14 '22 at 12:16
  • 1
    Actually there was some internal problem in constructing the parameters from the external program which led to this confusion. Now it is solved. Thanks for the patience and suggestion :) – Sandeep Bhat Mar 14 '22 at 12:54
  • @SandeepBhat That's great to hear, glad you got it working! :) – Mathias R. Jessen Mar 14 '22 at 15:34