Public/Out-ISETab.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Function Out-ISETab {
  Param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [psobject]$InputObject,
    [ValidateScript({
          if ($_ | Test-Path) {
            throw 'Choose a path that does not exist.'
          }
          return $true
    })]
    [System.IO.FileInfo]$SaveAs
  )
  Begin {
    if(-not ($psISE)){ throw 'Must run in PowerShell ISE' }
    $Data = @()
  }
  Process {
    $Data += $InputObject
  }
  End {
    $NewFile = $psISE.CurrentPowerShellTab.Files.Add()
    $NewFile.Editor.InsertText(($Data | Out-String))
    $NewFile.Editor.SetCaretPosition(1,1)
    if ($SaveAs) {
      Write-Verbose -Message ('Saving to: {0}' -f $SaveAs) -Verbose:$VerbosePreference
      $NewFile.SaveAs($SaveAs.FullName,[System.Text.UTF8Encoding]::new($false))
    }
  }
}