Question:
In the folder E:\Files there are 2 files, which I want to copy and paste into D:\Dest:
E:\Files\
- File1.txt
- File2.txt
Using the keyboard I would simply select the 2 files, push ctrl+c, and in the destination folder D:\Dest\ I then push ctrl+v.
Now I want to achieve this using Powershell. So I copy the files into the clipboard:
1 2 |
Set-Clipboard -Path E:\Files\* |
But how to paste those files now into the destination folder? Obviously, I will need Get-Clipboard
. But it’s not quite clear to me, how to use it, in order to paste the files.
I know I could copy the contents of these 2 files and then by using Set-Content
create those files in D:\Dest\ on my own and copy the content into them. But is there any direct way? Because with Set-Clipboard
those files already are in the clipboard. They just need to be pasted. I can use ctrl+v and it works. But I want to paste them through Powershell. Any ideas?
Answer:
Here’s the script I use to paste files/directories:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$fileDrop = get-clipboard -Format FileDropList if($fileDrop -eq $null) { write-host "No files on the clipboard" return } foreach($file in $fileDrop) { if($file.Mode.StartsWith("d")) { $source = join-path $file.Directory $file.Name $e = "copy-item -Recurse $source $($file.Name)" $e Invoke-Expression $e } else { $file.Name $file | copy-item } } |