Created
May 14, 2019 14:14
-
-
Save adbertram/4e4bf0ba5f876ed474f90534520cf2e2 to your computer and use it in GitHub Desktop.
This file contains 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 Save-ConsoleOutputToClipBoard { | |
[OutputType('string')] | |
[CmdletBinding()] | |
param | |
() | |
if ($host.Name -ne ‘ConsoleHost’) { | |
write-host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)." | |
} | |
# Initialize string builder. | |
$textBuilder = new-object system.text.stringbuilder | |
# Grab the console screen buffer contents using the Host console API. | |
$bufferWidth = $host.ui.rawui.BufferSize.Width | |
$bufferHeight = $host.ui.rawui.CursorPosition.Y | |
$rec = new-object System.Management.Automation.Host.Rectangle 0, 0, ($bufferWidth – 1), $bufferHeight | |
$buffer = $host.ui.rawui.GetBufferContents($rec) | |
# Iterate through the lines in the console buffer. | |
for($i = 0; $i -lt $bufferHeight; $i++) { | |
for($j = 0; $j -lt $bufferWidth; $j++) { | |
$cell = $buffer[$i, $j] | |
$null = $textBuilder.Append($cell.Character) | |
} | |
$null = $textBuilder.Append("`r`n") | |
} | |
## Ensure the PS prompt is always just PS> | |
$out = $textBuilder.ToString() -replace 'PS .*\>', 'PS>' | |
## Remove the line that actually invoked this function | |
$out -replace "PS> $($MyInvocation.MyCommand.Name)" | Set-Clipboard | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like it!