Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Created February 12, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazywinadmin/ff8717a1c1b393305ff8 to your computer and use it in GitHub Desktop.
Save lazywinadmin/ff8717a1c1b393305ff8 to your computer and use it in GitHub Desktop.
Send the output to Notepad
#requires -Version 2
#http://powershell.com/cs/blogs/tips/archive/2016/02/11/send-text-to-notepad.aspx
function Out-Notepad
{
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[String]
[AllowEmptyString()]
$Text
)
begin
{
$sb = New-Object System.Text.StringBuilder
}
process
{
$null = $sb.AppendLine($Text)
}
end
{
$text = $sb.ToString()
$process = Start-Process notepad -PassThru
$null = $process.WaitForInputIdle()
$sig = '
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
'
$type = Add-Type -MemberDefinition $sig -Name APISendMessage -PassThru
$hwnd = $process.MainWindowHandle
[IntPtr]$child = $type::FindWindowEx($hwnd, [IntPtr]::Zero, "Edit", $null)
$null = $type::SendMessage($child, 0x000C, 0, $text)
}
}
#Get-Content $env:windir\system32\drivers\etc\hosts | Out-Notepad
#Get-Process | Out-String | Out-Notepad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment