Skip to content

Commit 4037b4a

Browse files
committed
tip: Add tip for Start-Transcript
1 parent 44a69db commit 4037b4a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.

src/tiPS/PowerShellTips.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,5 +854,19 @@
854854
"Category": 2,
855855
"ExpiryDate": "9999-12-31T23:59:59.9999999",
856856
"Author": "Daniel Schroeder (deadlydog)"
857+
},
858+
{
859+
"CreatedDate": "2025-01-08T00:00:00",
860+
"Title": "Use Start-Transcript for a quick and easy log file",
861+
"TipText": "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.\r\n\r\nIf 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.\r\n\r\nIt 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.",
862+
"Example": "try {\r\n # Log all script output to a file for easy reference later if needed.\r\n # $PSCommandPath will be the file path of the PowerShell script that is running.\r\n # Include the date and time in the log file name to make it unique instead of overwriting it every run.\r\n [string] $lastRunLogFilePath = \"$PSCommandPath.LastRun.log\"\r\n Start-Transcript -Path $lastRunLogFilePath\r\n\r\n # Put your script code here...\r\n}\r\nfinally {\r\n Stop-Transcript\r\n}",
863+
"Urls": [
864+
"https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript",
865+
"https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/stop-transcript",
866+
"https://lazyadmin.nl/powershell/start-transcript/"
867+
],
868+
"Category": 3,
869+
"ExpiryDate": "9999-12-31T23:59:59.9999999",
870+
"Author": "Daniel Schroeder (deadlydog)"
857871
}
858872
]

0 commit comments

Comments
 (0)