在 Powershell 中通过剪贴板复制并粘贴文件

She*_*don 5 clipboard powershell copy-paste file

在文件夹 E:\Files 中有 2 个文件,我想将其复制并粘贴到 D:\Dest 中:

E:\文件\

  • 文件1.txt
  • 文件2.txt

使用键盘,我只需选择 2 个文件,按 ctrl+c,然后在目标文件夹 D:\Dest\ 中按 ctrl+v。

现在我想使用 Powershell 来实现这一点。所以我将文件复制到剪贴板中:

Set-Clipboard -Path E:\Files\*
Run Code Online (Sandbox Code Playgroud)

但是现在如何将这些文件粘贴到目标文件夹中呢?显然,我需要Get-Clipboard. 但我不太清楚如何使用它来粘贴文件。

我知道我可以复制这两个文件的内容,然后自己Set-Content在 D:\Dest\ 中创建这些文件并将内容复制到其中。但有没有直接的办法呢?因为Set-Clipboard这些文件已经在剪贴板中了。它们只需要粘贴即可。我可以使用 ctrl+v 并且它可以工作。但我想通过 Powershell 粘贴它们。有任何想法吗?

小智 1

在 PowerShell 6 之前(当 Get-Clipboard -Format 被删除时),要粘贴文件/目录:

$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
    }
}

Run Code Online (Sandbox Code Playgroud)