Command Line File to Clipboard: How to Copy Files via CLI Like Windows Explorer’s Context Menu


2 views

Most clipboard managers focus on text operations rather than file system objects. While tools like NirCmd and Clipboard.exe exist, they primarily handle text data or lack clear documentation for file operations. What we need is the equivalent of right-clicking a file in Explorer and selecting "Copy".

Here's a PowerShell script that adds files to the clipboard just like Explorer's copy operation:

function Copy-ToClipboard {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path
    )

    Add-Type -AssemblyName System.Windows.Forms
    $fileList = New-Object System.Collections.Specialized.StringCollection

    if (Test-Path $Path) {
        $fileList.Add((Resolve-Path $Path)) | Out-Null
        [System.Windows.Forms.Clipboard]::SetFileDropList($fileList)
    } else {
        Write-Error "File not found: $Path"
    }
}

# Usage:
# Copy-ToClipboard "C:\boot.ini"

For better performance in batch operations, here's a C# version you can compile:

using System;
using System.Windows.Forms;
using System.Collections.Specialized;

class ClipFile {
    static void Main(string[] args) {
        if (args.Length == 0) {
            Console.WriteLine("Usage: clipfile.exe [filepath]");
            return;
        }

        try {
            StringCollection paths = new StringCollection();
            paths.Add(System.IO.Path.GetFullPath(args[0]));
            Clipboard.SetFileDropList(paths);
            Console.WriteLine($"Copied to clipboard: {args[0]}");
        } catch (Exception ex) {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

For those who prefer simple batch commands:

@echo off
powershell -command "Add-Type -AssemblyName System.Windows.Forms; $col=New-Object System.Collections.Specialized.StringCollection; $col.Add('%CD%\%1'); [System.Windows.Forms.Clipboard]::SetFileDropList($col);"

Save as clip.bat and use: clip.bat boot.ini

If you prefer standalone executables:

  • Clip (Windows SDK): clip < boot.ini (text only)
  • WinClip: Supports multiple formats including files
  • AutoHotkey: Can create custom file-to-clipboard scripts

For developers needing more control:

// C# example with multiple files
var files = Directory.GetFiles(@"C:\Config\", "*.ini");
StringCollection paths = new StringCollection();
paths.AddRange(files);
Clipboard.SetFileDropList(paths);

Remember that clipboard operations require STA apartment state in .NET applications. For PowerShell, this means running the console in STA mode or using -STA parameter.


When automating workflows, developers often need to programmatically copy files to the Windows clipboard - equivalent to right-clicking a file in Explorer and selecting "Copy". This enables seamless pasting into other applications. Let's explore robust solutions.

Windows PowerShell provides the most straightforward method without third-party tools:

# Single file copy
Add-Type -AssemblyName System.Windows.Forms
$file = "C:\\boot.ini"
[Windows.Forms.Clipboard]::SetFileDropList([Collections.Specialized.StringCollection]@($file))

# Multiple files example
$files = @("C:\\file1.txt", "D:\\data\\config.ini")
[Windows.Forms.Clipboard]::SetFileDropList($files)

For batch files or environments without PowerShell, NirSoft's nircmd works reliably:

:: Single file
nircmd.exe clipboard copyfile "C:\boot.ini"

:: Multiple files
nircmd.exe clipboard copyfiles "C:\file1.txt" "C:\file2.log"

For Python-based automation scripts:

import win32clipboard
from io import StringIO
import os

def copy_files_to_clipboard(filepaths):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    
    # Format file list for clipboard
    files = "\0".join([os.path.abspath(f) for f in filepaths])
    data = files.encode("utf-16le") + b"\0\0"
    
    win32clipboard.SetClipboardData(win32clipboard.CF_HDROP, data)
    win32clipboard.CloseClipboard()

# Usage
copy_files_to_clipboard(["C:\\boot.ini", "D:\\config.cfg"])

Windows uses CF_HDROP format for file operations. The data structure includes:

  • Double-null terminated Unicode file paths
  • Full absolute paths required
  • Supports multiple files separated by null characters

Common issues and fixes:

:: Verify clipboard contents
nircmd.exe clipboard saveclipboard "C:\\clipdata.txt"

:: Clear clipboard if operations fail
nircmd.exe clipboard clear