Author Posts

September 26, 2014 at 8:25 am

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.

(pre)$MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
$Today = Get-Date
Write-host ""
Write-host ""
Write-host ""
Write-host "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"
Write-host ""
get-acl "\\$MyACLs\c$\windows\system32\winevt\Logs\" -verbose | select *
Write-host ""
Write-host "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"(/pre)

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.)

September 26, 2014 at 8:36 am

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)

September 26, 2014 at 8:41 am

Thank you.

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.

September 26, 2014 at 8:43 am

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.

September 26, 2014 at 8:47 am

Thanks for your input. I'll continue doing research on this topic.

September 26, 2014 at 4:24 pm

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.

(pre)$MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
$Today = Get-Date
$hf = "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"
$ga = get-acl "c:\$MyACLs" -verbose | select *

Function Concat($a){
Concat $hf,$ga,$hf | out-file C:\Temp\temp.txt(/pre)

September 30, 2014 at 5:10 am

Have you considered using a here-string? You can format the text in a WYSIWYG format and just output the variable to a text file:

$MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
$Today = Get-Date
$ACL = get-acl "\\$MyACLs\c$\windows\system32\winevt\Logs\" -verbose | Select *

$hereString = @"

### AC-09 = Security Log ACLs – $MyACLs – $Today ###


### AC-09 = Security Log ACLs – $MyACLs – $Today ###

$hereString | Out-File C:\MyAcl.txt