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%" %*
2
  • 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(" ") Commented Mar 14, 2022 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. Commented Mar 14, 2022 at 11:57

1 Answer 1

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 ,

Sign up to request clarification or add additional context in comments.

8 Comments

I have already tried this like I mentioned in my question(the links), unfortunately I still get the same output.
@SandeepBhat It should work with C:\Sample.cmd C:\file1.txt C:\file2.txt (or C:\Sample.cmd "C:\file1.txt" "C:\file2.txt")
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 :(
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
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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.