Write stdout to screen and file [duplicate] - powershell

This question already has answers here:
How can I output Handbrake output to both the screen and to a file?
(2 answers)
Closed 7 years ago.
When running a powershell script from the powershell console, I can redirect the output to a file like this:
powershell script.ps1 > file.txt
However, I need to redirect the output to a file AND still print it to the screen. How can I do this?

Use a transcript. You can do it one of two ways, the easiest is probably including these lines in your script:
In "Script.ps1"
Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript
The second way would be to call your script like so:
Start-Transcript "C:\file.txt"
.\Script.ps1
Stop-Transcript
Keep in mind that if you try to start a transcript, without the previous transcript not stopped, it will throw an error. So in your script, prior to the Start-Transcript command, maybe do this just to ensure the transcript isn't still running:
try{
Stop-Transcript
}catch{}
Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript

Related

Powershell printing global variable to the log file after transcript is stopped

I am running simple application where I start transcript from one file, continue it using -Append option in another file and then end it.
Everything works fine, it logs everything, the only problem is it puts these two lines also in the log file by itself
PS>$global:?
True
right before the end transcript lines
**********************
Windows PowerShell transcript end
End time: 20210507200412
**********************
Any idea what could be the reason?
Im not familiar with the syntax, but the statement $global:? does indeed evaluate to $true, as to why your code is emitting $global:? i cant say.
Ive just tested the following code under PowerShell 5(Windows/ ISE) and 7(Ubuntu WSL Powershell Core) and i dont get the same output as you so i suspect something in your code is outputting $global:? which results in the 2 lines in question? (it could something in your profile etc not necessarily in the code your looking at)
If you run the below sample do you still see the unexpected lines in the output?
$filename = "./transcript-test.log";
Start-Transcript -Path $filename
Write-Host "This is me writing some stuff to the screen..."
Stop-Transcript
Start-Transcript -Path $filename -Append
Write-Host "Writing more stuff to the console after reopening/appending transcript"
Stop-Transcript
Get-Content $filename
Make sure you do a Stop-Transcript before your script ends.

Redirect the output of a PowerShell script to a file using command prompt

How to redirect the output of a PowerShell 5.0 script to a file using cmd in Windows 10? I tried the following:
powershell ".\myscript.ps1 | Out-File outfile.txt"
and:
powershell .\myscript.ps1 > outfile.txt
to no avail. The outfile.txt is created but remains empty. The command prompt window is run with Administrator privileges.
In a script I use:
Write-Host $MyVar
Write-Host $SomeOtherVar
to output the values to a screen.
Use the specific PowerShell redirection operators:
(they appear to work at the (cmd) command prompt as wel)
Redirect the success stream (1>):
powershell .\myscript.ps1 1> outfile.txt
Redirect all streams (*>):
powershell .\myscript.ps1 *> outfile.txt
In a script I use:
Write-Host $MyVar
Write-Host $SomeOtherVar
to output the values to a screen.
Yeah, that's your problem right there! Write-Host writes the information straight to the screen buffer of the host application, so the standard output stream will never actually see anything.
Change your Write-Host statements to Write-Output (or just remove them):
Write-Output $MyVar
Write-Output $SomeOtherVar
# or simply
$MyVar
$SomeOtherVar

Powershell pipe all to log

I'm very new to powershell and I'm looking to pipe all lines to an output file for logging with it's time, but I would rather not explicitly pipe all cmdlets to the log file to avoid visual clutter and to make the code easily extensible.
I'm not sure whether existing methods do what I'm asking and I just can't recognize it or what but any pointers or help would be appreciated.
If you want to capture all of the output generated in a PowerShell session (including other streams such as verbose or error messages) you can use Start-Transcript at the start of your script and Stop-Transcript at the end.
If you want to output specific log entries, consider using a Write-Log function as described in one of the answers here: Create Log File in Powershell
Or here's a simple version to get your started:
function Write-Log {
Param(
$Message,
$Path = "$env:USERPROFILE\log.txt"
)
function TS {Get-Date -Format 'hh:mm:ss'}
"[$(TS)]$Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
}
Write-Log 'Some message'
This adds a timestamp to the start of the message and directs it to the Verbose output stream as well as a file via Tee-Object. You can then see the Verbose messages if you enable them via $VerbosePreference (or by supporting -Verbose in your script).
Alternatively, if you want to redirect all output from your script that isn't the other message streams (e.g just things sent to the standard pipeline) you could save the script, run it from within PowerShell and then pipe its output to another command such as Out-File, e.g:
.\yourscript.ps1 | Out-File output.txt

Powershell write output of a program to a console live and pipe to a file

I am running a fairly complex python script from a powershell script. I would like to both display the stdout and stderr streams of the .python script in the console in real time (ie as the .py script writes to them) and also write them to a file.
My current solution looks like this:
Do-PreliminaryStuff
log = & ".\myPyScript.py" -myArgument "\this\that\thingy.txt" 2>&1 $log
Write-Output $log
$log | Out-File "$logDir\pyScriptLog.log"
Do-RestOfScript
This has the problem that the text is only printed out after the .py script has finished, making it much harder to watch the progress of the .py script.
Is there a way to somehow ..sample.. the pipeline as object go through it?
You are probably looking for the Tee-Object cmdlet.
Use Tee-Object - it splits the pipeline into a filestream before continuing:
& ".\myPyScript.py" -myArgument "\this\that\thingy.txt" 2>&1 |Tee-Object -FilePath $log

Trying to output powershell "Add-Computer" command to text file

I'm running a .PS1 script as part of a larger script to get computers to join our domain semi-automatically. The .PS1 is created from variables and ends up looking like this:
add-computer -DomainName ourdomain.com - OUPath "OU=Computers,OU=Somewhere,DC=OURDOMAIN,DC=COM" -Cred OD\syswdg
While this works fine, I would like to be able to output the restult of this to a text file so that I can check if this has worked sccessfully or not before proceeding to do other stuff in the script. Is there any way to get the results of this output to a file? I've tried using the Out-File Cmdlet, the Tee-Object Cmdlet and tried running the joindomain.PS1 from another PS1 like joindomain.ps1 > outputfile.txt and while they all produce a file, it is always empty. Any help appreciated.
By default, there is no output when the cmdlet is successful. Use -Passthru and -Verbose if you want to see the output of this cmdlet.