stash/Out-Notepad.ps1

function Out-Notepad {
  <#
    .SYNOPSIS
        Redirect string data to the Notepad.
    .DESCRIPTION
        `Out-Notepad` is useful when you want to edit the contents of a
        file without changing the original. But be warned, if the Notepad
        has been removed or replaced with an alternative text processor,
        an exception will occur.
        Some alternative text processor, such as Vim, have the feature of
        insertion from a pipeline, for example:
        echo test string | vim -
    .INPUTS
        System.String[]
    .OUTPUTS
        None
    .EXAMPLE
        Out-Notepad 'test', 'string'
    .EXAMPLE
        Get-Content .\file.txt | Out-Notepad
    .EXAMPLE
        Get-Content .\file.txt -Raw | Out-Notepad
        Same that above.
    .NOTES
        MIT
    .LINK
        None
  #>

  [CmdletBinding()]
  param(
    [Parameter(Mandatory, ValueFromPipeline)]
    [AllowEmptyString()]
    [String[]]$Text
  )

  begin {
    New-Delegate user32 {
      ptr FindWindowExW([ptr, ptr, buf, buf])
      int SendMessageW([ptr, uint, ptr, buf])
    }
  }
# process {}
  end {
    $notepad = Start-Process notepad -PassThru
    [void]$notepad.WaitForInputIdle()
    [void]$user32.SendMessageW.Invoke(
      $user32.FindWindowExW.Invoke(
        $notepad.MainWindowHandle, [IntPtr]::Zero, [buf].Uni('Edit'), $null
      ), 0xC, 0, [buf].Uni((
        ($Text, $input)[[Int32]$MyInvocation.ExpectingInput] -join "`n"
      ))
    )
    $notepad.Dispose()
  }
}