WSL is good, it will be even better if we can pipe stdout directly into Windows hostโs clipboard. Just like
./myscript.sh | to-my-clipboard.shNote that xclip requires X, so it is not an option. From WSL, we have direct access to Windows executables. The easiest method would be using clip.exe:
./myscript.sh | clip.exeThe method works fine if you donโt touch CJK characters or emojis. clip.exe didnโt handle the encoding well since the default codepage in Windows is not 65001(UTF-8). The command below would probably break
echo ๐ | clip.exeThe solution is to provide clip.exe with an environment in which the encoding can be specified. PowerShell is a natural fit for the job in Windows. We can do the trick with two simple steps:
- Setup PowerShell profile The PowerShell profile is like
.bashrcbut for PowerShell. You may need to create a.ps1file in the location of$PROFILE.echo $PROFILEin PowerShell would give you the full path of the profile, something likeC:\Users\\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. Ensure the path andnotepad.exe $PROFILElet you edit the file with notepad. Put the content in the file
chcp 65001 | Out-Null2. Use clip.exe with PowerShell in WSL With the setup above, you may simply pipe the result of any command from WSL to your Windows.
# with powershell 5.1
echo ๐ | powershell.exe -Command clip.exe
# with powershell 7.x
echo ๐ | pwsh.exe -Command clip.exeYou can also wrap it in a bash script
echo 'pwsh.exe -Command clip.exe' > ~/.local/bin/clip.sh
chmod +x ~/.local/bin/clip.shThen you can write
echo ๐ | clip.shNote that, I have also looked into Set-Clipboard cmdlet in the PowerShell. But I never got to escape the content passing through the WSL-PowerShell pipe right.