PowerTip: Redirect PowerShell Warning Messages to a Text File

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

Comments (9)

  1. jrv says:


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

  2. Tim says:

    Is the entire Warning Message supposed to be directed to the .txt file or just what you specify in Write-Warning ""   ?

  3. Vern_Anderson says:

    #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}

  4. ed wilson says:

    @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 …..

  5. ed wilson says:

    @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

  6. JN says:

    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?

  7. Mike Compton says:

    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?



  8. Mike Compton says:

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

    [PS] C:UsersAdministratorDocumentsWindowsPowerShell>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:UsersAdministratorDocumentsWindowsPowerShell>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


    [PS] C:UsersAdministratorDocumentsWindowsPowerShell>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

  9. Kevin says:

    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

Skip to main content