PowerTip: Redirect PowerShell Warning Messages to a Text File

PowerTip: Redirect PowerShell Warning Messages to a Text File

  • Comments 9
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 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

  • For the life of me, I can't get this to work.  Basically, I'm trying to use this against Exchange's get-mailbox cmdlet so that I can capture mailboxes that have some sort of issue, as a warning is thrown when the cmdlet encounters one of these mailboxes.

    I've tried both get-mailbox -resultsize unlimited 3> C:\Warnings.txt

    and encapsulated the above (minus the redirection) into a script:  getmbx.ps1 3> C:\Warnings.txt

    No matter what, the file turns out blank.  If I use a different redirector, say *> I can see the successes, but nothing I do will capture the warnings, and it's driving me crazy.

    Any ideas?

  • I am trying the same thing as JN but using: get-mailpublicfolder

    I need to somehow suppress the warnings.  Suppress is great, dump to NUL is great, dump to File is good, but not great.

    It seems the exchange cmdlets do not have coding to work w/ the Warning Preference.  I have try to set the variables:

    $WarningActionPreference = "SilentlyContinue"

    $WarningPreference = "SilentlyContinue"

    and common parameter: -WarningAction silentlyContinue

    But, I always see the warnings on the console.

    Any thoughts?

    Thanks,

    Mike

  • Sorry, I forgot to show some commands I have tried:

    [PS] C:\Users\Administrator\Documents\WindowsPowerShell>get-mailpublicfolder '\710003 - ABC, Test' | fl alias

    Alias : 710003 - ABC, Test

    WARNING: The object CartGolf.Org/Microsoft Exchange System Objects/710003 - ABC, Test has been corrupted, and it's in an inconsistent state. The following validation errors happened:

    WARNING: Property expression "710003 - ABC, Test" isn't valid. Valid values are: Strings formed with characters from A to Z (uppercase or lowercase), ...

    Here I get errors when using 3>

    [PS] C:\Users\Administrator\Documents\WindowsPowerShell>get-mailpublicfolder '\710003 - ABC, Test' 3> fred.txt

    A positional parameter cannot be found that accepts argument '3>'.

       + CategoryInfo          : InvalidArgument: (:) [Get-MailPublicFolder], ParameterBindingException

       + FullyQualifiedErrorId : PositionalParameterNotFound,Get-MailPublicFolder

       + PSComputerName        : kenny-exch2010.cartgolf.org

    Or

    [PS] C:\Users\Administrator\Documents\WindowsPowerShell>get-mailpublicfolder '\710003 - ABC, Test' | fl alias 3> fred.txt

    fl : A positional parameter cannot be found that accepts argument '3>'.

    At line:1 char:48

    + get-mailpublicfolder '\710003 - ABC, Test' | fl <<<<  alias 3> fred.txt

       + CategoryInfo          : InvalidArgument: (:) [Format-List], ParameterBindingException

       + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.FormatListCommand

  • I'm hitting the same error as well:

    PS C:\> Write-Warning "this is warning" 3> foo

    Write-Warning : A positional parameter cannot be found that accepts argument '3>'.

    At line:1 char:14

    + Write-Warning <<<<  "this is warning" 3> foo

       + CategoryInfo          : InvalidArgument: (:) [Write-Warning], ParameterBindingException

       + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteWarningCommand

  • @ALL

    Just a small note.  This only works in PowerShell V3 or later.