PowerShell Log-Files: Logging into a textfile - write to file

Diese Seite gibt es auch in Deutsch

Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.


A very simple variant to write the command line output into a text file is offered by the PowerShell command Transcript.


After it, the console output is written to a file. Once finished, the output can be stopped:


Transcript writes a header with information about the user and computer and then the complete console output to a text file:

Windows PowerShell transcript start
Start time: 20220505062335
Username: Test\LiBe
RunAs User: Test\LiBe
Configuration Name: 
Machine: TEST (Microsoft Windows NT 10.0.22000.0)
Host Application: powershell
Process ID: 8756
PSVersion: 5.1.22000.282
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22000.282
BuildVersion: 10.0.22000.282
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
Transcript started, output file is C:\Users\LiBe\Documents\PowerShell_transcript.TEST.1LdRFamK.20220505062335.txt
PS C:\Users\LiBe> write-host "test"
PS C:\Users\LiBe> stop-transcript
Windows PowerShell transcript end
End time: 20220505062400

Transcript for all PowerShell sessions and scripts

If you want to collect all PowerShell commands on a computer or server in transcripts, you can activate the transcript function in the registry. To do this, copy the following content into a .reg file and then import it by double-clicking on it.

Windows Registry Editor Version 5.00



For PowerShell Core a different registry path is used at this point, accordingly PowerShell Core needs its own entry in the registry. The path for the logs can of course be adjusted accordingly.

Alternatively, the setting can also be set via a group policy. A UNC share on the network could also be used as the path.

All PowerShell sessions log their output to a separate file in the specified folder:


If you want to take the output of the log lines into a text file into your own hands, you can do that with the command Out-File, see also: PowerShell-Textfile. With the help of the command Out-File it is possible to write from PowerShell to a text file, here is a simple example: 

"log line entry" | out-file "c:\temp\log.txt" -Append

The -Append parameter creates the file if it does not exist and adds additional log lines when called again.

So that, as is usual for a log file, the time is also written at the beginning of the log line, this can be added with the following command:

"$(get-date -format "yyyy-MM-dd HH:mm:ss"): log line entry" | out-file "c:\temp\log.txt" -Append

Output to file:

own logging function

So that the command for the output of the log line does not always have to include the command for the date, a separate function can also be created for logging. The function could then also implement things like an automatic naming of the log file, log rotation or similar.

To stay with the out-file example and to keep the function as simple as possible, I put the command into a small function: the write operation to the log file is then done using "Write-Log -text "Logtext", here is the function and its call:

function Write-Log

    "$(get-date -format "yyyy-MM-dd HH:mm:ss"): $($text)" | out-file "c:\temp\log.txt" -Append

Write-Log -text "Logline1"
Write-Log -text "Logline2"
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Top articles in this section

PowerShell Loops and Array

PowerShell Loops and Array

created: 2022-05-02 from Bernhard

An array stores multiple values, similar to a 2-column table. ... continue reading

Preview PowerShell text file and csv read / write

PowerShell text file and csv read / write

created: 2022-05-02 from Bernhard

PowerShell text file and csv read / write ... continue reading

Preview PowerShell - Handling passwords.

PowerShell - Handling passwords.

created: 2022-05-02 from Bernhard

Of course, passwords should never be stored in plain text in a script file. PowerShell offers possibilities for a secure handling of passwords. As an example, a password for a specific user can be stored encrypted as a text file. Only the respective user can decrypt the file, for other users the file is worthless. ... continue reading

Questions / Comments