rhythmo
rhythmo

Reputation: 949

Using Set-Clipboard to copy multiple files in PowerShell

I am trying to copy multiple file paths using "Set-Clipboard" command in PowerShell

Here is the code to copy multiple files that does not work

#Individual paths
$a = "C:\Users\me\test\test1.pdf"
$b = "C:\Users\me\test\test2.pdf"

$paths = '"' + $a + '"' + ', ' + '"' + $b + '"'

#Checking path
Write-Host $paths

#Copying to Clipboard
Set-Clipboard -Path $paths

But the following code works. The following code copies both "test1.pdf" and "test2.pdf" from their respective locations to clipboard

Set-Clipboard -Path "C:\Users\me\test\test1.pdf", "C:\Users\me\test\test2.pdf"

But when this string is generated by code, it does not work.

The following also works

Set-Clipboard -Path $a, $b

In my case, there are a lot of files to copy from different locations. So, I have to generate a string with paths separated by a comma.

Could someone point me in the right direction?

Or please suggest an alternate way to copy multiple file paths to clipboard. Thanks

Upvotes: 2

Views: 1051

Answers (1)

mklement0
mklement0

Reputation: 439257

$paths = '"' + $a + '"' + ', ' + '"' + $b + '"' constructs a single string, not an array, the latter being what Set-Clipboard's -Path parameter[1] expects.

Use $paths = $a, $b instead, via ,, the array constructor operator.

Explicit double-quoting of values stored in variables is never necessary in PowerShell, not even for values that contain spaces.


[1] Note that the cross-platform PowerShell (Core) v6+ edition no longer supports this parameter; it only supports copying text (strings), presumably due to only providing the least-common-denominator functionality across all platforms. See GitHub issue #14758 for a discussion.

Upvotes: 4

Related Questions