Skip to content

Instantly share code, notes, and snippets.

@everydayintech
Last active June 30, 2026 08:44
Show Gist options
  • Select an option

  • Save everydayintech/1186efc0152cd359c60cd9bb8160735b to your computer and use it in GitHub Desktop.

Select an option

Save everydayintech/1186efc0152cd359c60cd9bb8160735b to your computer and use it in GitHub Desktop.
Copy files as text with system clipboard
function ClipFile {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$FilePath
)
try {
$Item = Get-Item -Path $FilePath -ErrorAction Stop
}
catch {
Write-Warning "Cannot find path '$($FilePath)'"
return
}
$bytes = [System.IO.File]::ReadAllBytes($Item.FullName)
$b64 = [System.Convert]::ToBase64String($bytes)
$payload = $Item.Name + ':' + $b64
$payload | Set-Clipboard
}
function PasteFile {
param(
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$FilePath
)
$payload = Get-Clipboard
try {
$items = $payload -split ':'
if ($items.Length -ne 2) {
throw [System.ArgumentException]"Invalid format"
}
$bytes = [System.Convert]::FromBase64String($items[1])
}
catch {
Write-Warning "Failed to decode clipboard content. Make sure to use ClipFile first."
return
}
$path = if ($FilePath) {
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)
}
else {
Join-Path $PWD.Path $items[0]
}
[IO.File]::WriteAllBytes($path, $bytes)
}
@everydayintech

everydayintech commented Jun 30, 2026 •

Copy link
Copy Markdown
Author

Add context menu entry to Windows Explorer

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\ClipFile]
"icon"="C:\\Program Files\\PowerShell\\7\\pwsh.exe"

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\ClipFile\command]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -WindowStyle Hidden -Command \"ClipFile '%1'\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\PasteFile]
"icon"="C:\\Program Files\\PowerShell\\7\\pwsh.exe"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\PasteFile\command]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -WindowStyle Hidden -Command \"PushLocation '%V'; PasteFile\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32]
@=""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment