|
| 1 | +$tip = [tiPS.PowerShellTip]::new() |
| 2 | +$tip.CreatedDate = [DateTime]::Parse('2025-01-08') |
| 3 | +$tip.Title = 'Use Start-Transcript for a quick and easy log file' |
| 4 | +$tip.TipText = @' |
| 5 | +You can use the Start-Transcript cmdlet to easily create a log file of your PowerShell session. This is useful for keeping a record of what you did during an interactive session, or in your scripts to log any output for troubleshooting purposes. |
| 6 | +
|
| 7 | +If you do not specify the log file path, the log file will be created in the user's Home/Documents directory with a timestamped filename. The log file will remain locked by the PowerShell process until Stop-Transcript is called, so be sure to call Stop-Transcript when you are done. |
| 8 | +
|
| 9 | +It is often preferable to log output to a central logging system, however, that often requires much more code and may be overkill for some scenarios. Logging to a local file with Start-Transcript is a quick and easy alternative to capture output for later reference. |
| 10 | +'@ |
| 11 | +$tip.Example = @' |
| 12 | +try { |
| 13 | + # Log all script output to a file for easy reference later if needed. |
| 14 | + # $PSCommandPath will be the file path of the PowerShell script that is running. |
| 15 | + # Include the date and time in the log file name to make it unique instead of overwriting it every run. |
| 16 | + [string] $lastRunLogFilePath = "$PSCommandPath.LastRun.log" |
| 17 | + Start-Transcript -Path $lastRunLogFilePath |
| 18 | +
|
| 19 | + # Put your script code here... |
| 20 | +} |
| 21 | +finally { |
| 22 | + Stop-Transcript |
| 23 | +} |
| 24 | +'@ |
| 25 | +$tip.Urls = @( |
| 26 | + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript' |
| 27 | + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/stop-transcript' |
| 28 | + 'https://lazyadmin.nl/powershell/start-transcript/' |
| 29 | +) |
| 30 | +$tip.Category = [tiPS.TipCategory]::NativeCmdlet # Community, Editor, Module, NativeCmdlet, Performance, Security, Syntax, Terminal, or Other. |
| 31 | +$tip.Author = 'Daniel Schroeder (deadlydog)' # Optional. Get credit for your tip. e.g. 'Daniel Schroeder (deadlydog)'. |
| 32 | +#$tip.ExpiryDate = [DateTime]::Parse('2024-10-30') # Optional. If the tip is not relevant after a certain date, set the expiration date. e.g. Announcing a conference or event. |
| 33 | + |
| 34 | +# Category meanings: |
| 35 | +# Community: Social events and community resources. e.g. PowerShell Summit, podcasts, etc. |
| 36 | +# Editor: Editor tips and extensions. e.g. VSCode, ISE, etc. |
| 37 | +# Module: Modules and module tips. e.g. PSScriptAnalyzer, Pester, etc. |
| 38 | +# NativeCmdlet: Native cmdlet tips. e.g. Get-Process, Get-ChildItem, Get-Content, etc. |
| 39 | +# Performance: Tips to improve runtime performance. e.g. foreach vs ForEach-Object, ForEach-Object -Parallel, etc. |
| 40 | +# Security: Security tips. e.g. ExecutionPolicy, Constrained Language Mode, passwords, etc. |
| 41 | +# Syntax: Syntax tips. e.g. splatting, pipeline, etc. |
| 42 | +# Terminal: Terminal shortcuts and tips. e.g. PSReadLine, Windows Terminal, ConEmu, etc. |
| 43 | +# Other: Tips that don't fit into any of the other categories. |
0 commit comments