简体   繁体   中英

How to pass an array to the arguments in Start-Process in Powershell?

I am writing a script to play certain files in a player. I use Get-ChildItem to get an array of file names. Then I want to use Start-Process to play these files. However, how can I add these file names to the arguments of the player program?

I used Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $selected_items but it seems it doesn't work and the files are not played.

Notice there are spaces in the file names.

Judging by the available command-line options , the following may work (I cannot personally verify):

/clipboard :Appends content(s) from clipboard into playlist and starts playback immediately.

# Copy the full names of the files of interest to the clipboard.
Set-Clipboard -Value $selected_items.FullName

# Launch the player and tell it to start playback of the files on the clipboard.
# Parameters -FilePath and -ArgumentList are positionally implied.
Start-Process 'C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe' /clipboard

There are file-arguments-based options such as /new , /insert , and /add , but it's unclear to me whether they automatically start playback (may depend on the application's configuration).

You can ForEach-Object :

Get-ChildItem . | ForEach-Object {Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $_.FullName}

You don't need start-process.

& "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" 
C:\Program` Files\DAUM\PotPlayer\PotPlayerMini64.exe
$env:path += ';C:\Program Files\DAUM\PotPlayer'; PotPlayerMini64

I can't test this myself, but it looks like switch /content can take an array of file paths joined by space characters.

$selected_items = Get-ChildItem -Path 'X:\pathtothefiles' -File | ForEach-Object {
    # quote the file paths
    '"{0}"' -f $_.FullName
}

$player = "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe"
Start-Process -FilePath $player /content ($selected_items -join ' ')

# or
# & $player /content ($selected_items -join ' ')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM