Last active
August 22, 2023 11:45
-
-
Save ethzero/47f657ca635752b5bdb45f99eae40182 to your computer and use it in GitHub Desktop.
Transferring binary content by way of clipboard via Powershell
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
## Powershell method of transfering small (< 1 MB) binary files via Clipboard | |
## | |
## NB: Unwise to attempt to encode binary files exceeding 1 MB due to excessive memory consumption | |
## Powershell 5.0> | |
# On the transmission end: | |
$Content = Get-Content -Encoding Byte -Path binaryfile.xxx | |
[System.Convert]::ToBase64String($Content) | Set-Clipboard | |
# On the receiving end | |
$Base64 = Get-Clipboard -Format Text -TextFormatType Text | |
Set-Content -Value $([System.Convert]::FromBase64String($Base64)) -Encoding Byte -Path binaryfile.zip | |
## Prior to Powershell 5.0 | |
# On the transmission end: | |
$Content = Get-Content -Encoding Byte -Path binaryfile.xxx | |
[System.Convert]::ToBase64String($Content) | clip | |
# On the receiving end: | |
# Paste the Base64 encoded contents in a text file manually: | |
$Base64 = Get-Content –Path binaryfile.xxx.base64_encoded.txt | |
Set-Content -Value $([System.Convert]::FromBase64String($Base64)) -Encoding Byte -Path binaryfile.zip | |
To make things much faster with big files, use [System.IO.File]::ReadAllBytes
rather than Get-Content
. Here is the difference of performance for a 2.5 MB file:
PS C:\> Measure-Command { [byte[]]$Bytes = Get-Content -Path "C:\Temp\BigFile.zip" -Encoding byte }
Days : 0
Hours : 0
Minutes : 0
Seconds : 47
Milliseconds : 28
Ticks : 470281306
TotalDays : 0.00054430706712963
TotalHours : 0.0130633696111111
TotalMinutes : 0.783802176666667
TotalSeconds : 47.0281306
TotalMilliseconds : 47028.1306
PS C:\> Measure-Command { [byte[]]$Bytes = [System.IO.File]::ReadAllBytes("C:\Temp\BigFile.zip") }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 24
Ticks : 247561
TotalDays : 2.86528935185185E-07
TotalHours : 6.87669444444444E-06
TotalMinutes : 0.000412601666666667
TotalSeconds : 0.0247561
TotalMilliseconds : 24.7561
As you can see, it makes a HUGE difference !
I created a powershell function of the above and i incorporated the suggestion from @fred-gagnon. Very useful, big thanks to both of you.
https://gist.github.com/JohanSelmosson/64c6542ee7bf0efa5ea286d4a9710ef9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm to copy files in and out of Citrix