Back in March I wrote about useful bits in my PowerShell profile. Recently I’ve wanted to take the results of what I’m doing into an editor. It’s easy enough to pipe a result into CLIP.EXE
which puts it in the clipboard, but it’s not too hard to use the object model of PowerShell ISE to do it directly.
Function Out-Edit {
Param ( [parameter(ValueFromPipeLine= $true)][Alias('Text')]$inputObject,
[String]$Before,
[String]$After,
[Switch]$New
) begin { if ($new) {$Editor = $psise.CurrentPowerShellTab.Files.Add().editor }
else {$Editor = $psise.CurrentFile.Editor }
if ($before) {$Editor.InsertText($before) }
}
process { $Editor.InsertText($inputObject ) }
end { if ($after) {$Editor.InsertText($after) } }
}
I gave the function a –new
switch to decide if the input goes into the current file or a new one. And to allow the input to be topped and tailed – typically with something like $Foo=@"
and "@
to make my output into a here-string , it’s just a case of getting a new or current file’s editor and calling its .InsertText()
method
Note: In that March piece I showed my replacement for the default PSEDIT function, in the end I merged it with the function above and I’ve put my profile.ps1 on skydrive. In the end I changed the NEW switch, to a “UseExisting” switch, because I found that I almost always needed the new option.