I recently wrote a post about how to create log files with PowerShell transcription capabilities.
What I like about PowerShell transcripts is simplicity. However, as I said in my former post if you want to get the most of it, you should use the Verbose parameter with each cmdlet you launch. Thus, when a warning or an error happens, you know in which context it happened.
But some cmdlets are very, very, very verbose… For example, FSRM cmdlets are some of those extremely verbose cmdlets. In such cases, instead of writing:
New-FSRMQuota -Path C:\FolderToMonitor -Template 'TemplateName' -Verbose
You probably want to write something like:
Write-Verbose -Message "New FSRM quota for $Path with template $TemplateName" New-FSRMQuota -Path $Path -Template $TemplateName
Unfortunately, if you did not a specific action before, nothing will be captured in the transcript file. Let’s see why…
What is captured
To have a global overview of the different possibilities you have here is an example of script.
Start-Transcript -Path C:\Temp\Transcript.log Write-Output 'Output message' Write-Warning -Message 'Warning message' Write-Debug -Message 'Debug message' Write-Verbose -Message 'Verbose message' Write-Information -MessageData 'Information message' Write-Host 'Host message' Write-Error -Message 'Error message' throw 'Throw exception' Stop-Transcript #this line should never been reached
As you can notice in the output, not everything goes to the console. Write-Debug
, Write-Verbose
, and Write-Information
don’t display anything.
And if you have a look at the log file, nothing has been capture neither for these three cmdlets.
Note: I was a little surprised because despite the Write-Host cmdlet does not go down the pipeline, it has still been captured by the transcript.
How to capture Information, Debug, and Verbose messages
In fact, the behavior we experienced above happens because of the default values of the preference variables.
Thus if you want to display and capture messages for Write-Debug
, Write-Verbose
and Write-Information
cmdlets, you just have to set their preference variables to Continue.
Start-Transcript -Path C:\Temp\Transcript.log $DebugPreference = 'Continue' $VerbosePreference = 'Continue' $InformationPreference = 'Continue' Write-Output 'Output message' Write-Warning -Message 'Warning message' Write-Debug -Message 'Debug message' Write-Verbose -Message 'Verbose message' Write-Information -MessageData 'Information message' Write-Host 'Host message' Write-Error -Message 'Error message' throw 'Throw exception' Stop-Transcript #this line should never been reached
As you can notice now, everything is captured.
Caveats
When you set preference variables inside a function, the impact is limited to the function.
However, when you set preference variables inside a script, they continue to apply to the current host even after the script has finished.
The Write-Verbose case
Depending on the cmdlets, scripts, and function you will use, preference variables will apply differently.
For example, with the New-Item
cmdlet, the $VerbosePreference variable set to Continue has no impact. As you can see in the following example, you have to use the Verbose parameter in order to get some verbose output.
Conversely, and as you can see in the following example, there is no need to use the Verbose parameter with the Import-Module
when the $VerbosePreference variable is set to Continue.
While setting the Information and Debug variable to Continue has no big impact, it becomes quickly an annoyance with the Verbose preference variable. Then how can we output and capture messages from the Write-Verbose
cmdlet without setting the $VerbosePreference variable to Continue?
Just use the Verbose parameter with the Write-Verbose
cmdlet!
$VerbosePreference = 'SilentlyContinue' Write-Verbose -Message "This is a verbose message without the verbose parameter" Write-Verbose -Message "This is a verbose message with the verbose parameter" -Verbose
Wrapping up
Based on my personal experience, to get the most of your transcript log files:
- Use the Verbose parameter with most, if not all, cmdlets
- Keep the $VerbosePreference variable set to SilentlyContinue and use the Verbose parameter with the
Write-Verbose
cmdlet - If needed, you can set the $DebugPreference and the $InformationPreference variables to Continue
And you? How do you get the most of your transcript log files?
Feel free to share your personal tips and tricks in the comment section below…
Hi Luke, have you ever encountered issues with Read-Host prompts and user input not being captured in the transcript?
LikeLike
Hi! Can you provide the line with the Read-Host?
LikeLike
$val = Read-Host “Enter value…”
I did some googling and it may be because I’m running on win server 2012 r2.
It’s not really important just wondering if you may have noticed anything like this.
LikeLike
The user input is not being captured because you store it in a variable.
Try this instead:
Read-Host -Prompt “Enter value” -OutVariable Val
The OutVariable parameter is very handy when you want to store a result into a variable while displaying the result on the screen at the same time.
But be aware that you must specify the variable name without the dollar sign.
This does NOT work:
Read-Host -Prompt “Enter value” -OutVariable $Val
LikeLike
Hey thanks for that, the user input now appears in the transcript. It’s interesting behavior. The user input is captured, but the prompt text is not.
LikeLike
There are several possible solutions.
For example:
‘Enter Value’
Read-Host -OutVariable Value
or
‘Enter Value’ | Tee-Object -Variable Prompt
Read-Host -Prompt $Prompt -OutVariable Value
LikeLike
Hi Luke, thanks for your post, it is very useful.
May I ask under what kind of possibilities that “Stop-Transcript” can never be reached?
Because I have no idea why my transcript log only contains Start-Transcript, but there is no “Windows PowerShell transcript end” at the end of the log file and the script is completed running.
LikeLike
Hi Chingsong,
This can happen when the last object you call does itself an improper exit, and thus also exits the PowerShell process which called it.
For example, you write a script with:
Start-Transcript
& C:\Folder\MyExecutable.exe
Get-Something
Stop-Transcript
If MyExecutable.exe or Get-Something are not properly ending (like a crash or exit 1, etc.) they will also kill the process of your script which called them and the Stop-Transcript will never been reached.
LikeLike
This is not working,Can you comment there why :
https://stackoverflow.com/questions/63054262/capture-verbose-output-of-powershell-cmdlet-running-on-a-remote-server
LikeLike
Nice article! online transcription service
LikeLike