PowerShell Tail – Watch log file as data is appended by script

Add this snippet to any script which writes to a log file, and it will open a second PowerShell console window with which you can watch the logfile as it is updated in real-time.

1
2
$Logfile = "$env:systemroot\Temp\$Logname.log"
Start-Process -FilePath $pshome\powershell.exe -ArgumentList @("-NoExit","&`{Get-Content -LiteralPath $Logfile -Tail 50 -Wait`}")

Start-Transcript – Execute only if in console and not ISE

Due to functionality differences between the PowerShell console and PowerShell ISE, there are times you only want to run certain functions and/or commands if the script is running in a console window. Below is an example of how to do so.

1
2
3
4
5
6
# Log script output to file - only works in console, not ISE.
If ($host.Name -notlike "*ISE*"){
    $scriptLog = "dcpromo_$(Get-Date -f yyyy-MM-dd_HH_mm_ss).log"
    $logging = Start-Transcript -Path $scriptLog
    Write-Host "`tTranscript started, logging to: $($logging.Path)" -ForegroundColor Cyan
}