<<< Back to Search

Scripting - Logging - Create Session Log

Create log with all information presented on PowerShell session with commands:

Start-Transcript / Stop-Transcript

Core Snippet & Sample

Copy text below into a power shell session and execute to see the code in action.

# Scripting - Logging - Create Session Log
# Core Snippet &amp; Sample
# www.PowerShellExamples.com

# Customize you Log file name
$logPathFile = "c:\temp\PowerShell_Session_$(Get-Date -Format yyyyMMdd_HHmmss).log"

Start-Transcript $logPathFile 

# Insert here commands to execute.  All information will be logged on file above
# Sample
Write-Host "`r`n`r`nThis information is being logged"

Write-Host "End Sample"


Stop-Transcript

# Optional: Open generated log at the end
Start-Process -FilePath Notepad.exe -ArgumentList $logPathFile


Result

PS C:\>

Transcript started, output file is c:\temp\PowerShell_Session_20220621_154004.log


This information is being logged
End Sample
Transcript stopped, output file is C:\temp\PowerShell_Session_20220621_154004.log