Should be a simple thing, but I’m stuck. I simply would like what is displayed on the screen in ISE to be sent to a text file at the end of the script. I’ve tried out-file, but creates a blank text file, the content that is on screen is not there.
I simply want what is outputed on screen to be saved as a .txt file in a specified path.
Right now, I’m copy/paste the screen into a text file (the extra lines at top with write-host, are simply so on screen I have a visual cue as to where start my copy/paste.)
You’ve come across one of the reasons why Write-Host is generally frowned on. Using things like Out-File, output from Write-Host is lost. You can try Start-Transcript when you’re using PowerShell.exe, but for now, that doesn’t work anywhere else. (PowerShell 5.0, currently in a preview release, has transcript support in all hosts, not just powershell.exe)
So no way to modify the code (ie, write-output), encapsulate it somehow to be piped to " > ", ect…?
I read about start-transcript, but haven’t tried it.
For my current workflow, this will work (copy/paste), just hoping to be able to apply a technique that produces that result to other scripts down the road.
The generally accepted best practice is to use Write-Output for objects that your script is returning, and Write-Verbose for any “screen-only” feedback. If your script’s only output is intended to be on the screen, rather than being assigned to a variable or piped to another command, then you can also just use Write-Output in place of either Write-Host or Write-Verbose. This makes it easy to redirect your script’s output with the redirection operators, Out-File, etc.
I don’t know if it’s the “proper” way to do so, but I’ve achieved what I need. I modified the code of the original post, but the concept is what I’m after, and it works, I have other scripts I plan to use this on (to avoid copy/paste from the ISE console to a text file) .Put a header/footer above the result of the get-acl command, and send it to a .txt file automatically.