Last active
June 30, 2026 08:44
-
-
Save everydayintech/1186efc0152cd359c60cd9bb8160735b to your computer and use it in GitHub Desktop.
Copy files as text with system clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add context menu entry to Windows Explorer