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

 

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

Transcript

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

start-transcript

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

stop-transcript 

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
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Users\LiBe\Documents\PowerShell_transcript.TEST.1LdRFamK.20220505062335.txt
PS C:\Users\LiBe> write-host "test"
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

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShellCore\Transcription]
"EnableTranscripting"=dword:00000001
"OutputDirectory"="c:\\windows\\temp\\pstranscripts"

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription]
"EnableTranscripting"=dword:00000001
"OutputDirectory"="c:\\windows\\temp\\pstranscripts"

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:

Out-File

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
{
    Param
    (
        $text
    )

    "$(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!

Publication: 2022-05-05 from Bernhard | Übersetzung Deutsch |🔔 | Comments:0

Send PowerShell Email: Send-MailMessage | PowerShell | PowerShell: File attributes: Change date - without any tools.

Top articles in this section


PowerShell: Prevent screen saver, lock: Move mouse regularly

Alternatively, if you can't change the screen lock settings, you can move the mouse regularly, or have a script move the mouse. Originally published as an AutoIt script, I recreated the script with a few PowerShell lines. Anyone who copies the following commands into a PowerShell session will prevent the computer from locking the screen or starting the screensaver:


PowerShell TCP Listener

PowerShell uses the following commands to open a socket on a specific port via System.NET:


PowerShell: File attributes: Change date - without any tools.

As an alternative to special programs, the date of a file or folder can also be changed with PowerShell. 

Questions / Comments