PowerTip: Redirect PowerShell Warning Messages to a Text File

PowerTip: Redirect PowerShell Warning Messages to a Text File

Rate This
  • Comments 4

Summary: Use the warning redirection operator to redirect Windows PowerShell warning messages to a text file.

Hey, Scripting Guy! Question How can you prevent warning messages from displaying to the Windows PowerShell host, but instead capture them in a text file?

Hey, Scripting Guy! Answer Use the warning message redirection operator:

$WarningPreference = "continue"

 Write-Warning "this is warning" 3> c:\fso\warning.txt

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
  • Is the entire Warning Message supposed to be directed to the .txt file or just what you specify in Write-Warning ""   ?

  • #off topic but it finally hit me this folder you always use

    Does fso = File System Object?

    if $answer -eq $True then "duuuude!!!"

    else {dim lightbulb}

  • @Vern_Anderson yes, it stands for file system object :-) it is just part of my sick sense of humor -- I love using an anachronistic reference to a legacy scripting language inside a modern scripting and automation environment. Besides, it is really easy to type --- I could call it ben I guess .....

  • @Tim it redirects the specific stream, so not all warnings will redirect, unless you place the redirection at the point where you call the script.

    myscript.ps1 3> warnings.txt

Page 1 of 1 (4 items)