Jeff
Jeff

Reputation: 36573

PowerShell redirect Information Stream to Verbose Stream from inside script

I'd like to redirect the Information Stream to the Verbose Stream from within my executing PowerShell script (not from the PowerShell command line).

As far as I know, the only way of getting a handle on the Information Stream object is via the System.Management.Automation.PowerShell instance. If leveraging an isolated PowerShell instance, I could add an event handler like so:

$psInstance.Streams.Information.add_DataAdding({
  param($sender, $e)  
  Write-Verbose $e.ItemAdded
})

But in my case, I'm looking to do this from the executing script....so if I could do something like:

$psInstance = [PowerShell]::Current

if the Current property actually existed...

So, my question is - can I get a handle on the currently executing System.Management.Automation.PowerShell instance or in lieu of that is there another way I can redirect the Information Stream to the Verbose stream from within my executing script?

Upvotes: 1

Views: 749

Answers (1)

js2010
js2010

Reputation: 27423

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-5.1

This is an example of it. 6>&4 doesn't seem to work.

write-host hi 6>&1 | Write-Verbose -Verbose 4>4.txt
cat 4.txt

hi

Upvotes: 2

Related Questions