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
  • <p>Is the entire Warning Message supposed to be directed to the .txt file or just what you specify in Write-Warning &quot;&quot; &nbsp; ?</p>

  • <p>#off topic but it finally hit me this folder you always use</p> <p>Does fso = File System Object?</p> <p>if $answer -eq $True then &quot;duuuude!!!&quot;</p> <p>else {dim lightbulb}</p>

  • <p>@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 .....</p>

  • <p>@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.</p> <p>myscript.ps1 3&gt; warnings.txt</p>

  • <p>For the life of me, I can&#39;t get this to work. &nbsp;Basically, I&#39;m trying to use this against Exchange&#39;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.</p> <p>I&#39;ve tried both get-mailbox -resultsize unlimited 3&gt; C:\Warnings.txt</p> <p>and encapsulated the above (minus the redirection) into a script: &nbsp;getmbx.ps1 3&gt; C:\Warnings.txt</p> <p>No matter what, the file turns out blank. &nbsp;If I use a different redirector, say *&gt; I can see the successes, but nothing I do will capture the warnings, and it&#39;s driving me crazy.</p> <p>Any ideas?</p>

  • <p>I am trying the same thing as JN but using: get-mailpublicfolder</p> <p>I need to somehow suppress the warnings. &nbsp;Suppress is great, dump to NUL is great, dump to File is good, but not great.</p> <p>It seems the exchange cmdlets do not have coding to work w/ the Warning Preference. &nbsp;I have try to set the variables: </p> <p>$WarningActionPreference = &quot;SilentlyContinue&quot;</p> <p>$WarningPreference = &quot;SilentlyContinue&quot;</p> <p>and common parameter: -WarningAction silentlyContinue</p> <p>But, I always see the warnings on the console.</p> <p>Any thoughts?</p> <p>Thanks,</p> <p>Mike</p>

  • <p>Sorry, I forgot to show some commands I have tried:</p> <p>[PS] C:\Users\Administrator\Documents\WindowsPowerShell&gt;get-mailpublicfolder &#39;\710003 - ABC, Test&#39; | fl alias</p> <p>Alias : 710003 - ABC, Test</p> <p>WARNING: The object CartGolf.Org/Microsoft Exchange System Objects/710003 - ABC, Test has been corrupted, and it&#39;s in an inconsistent state. The following validation errors happened:</p> <p>WARNING: Property expression &quot;710003 - ABC, Test&quot; isn&#39;t valid. Valid values are: Strings formed with characters from A to Z (uppercase or lowercase), ...</p> <p>Here I get errors when using 3&gt;</p> <p>[PS] C:\Users\Administrator\Documents\WindowsPowerShell&gt;get-mailpublicfolder &#39;\710003 - ABC, Test&#39; 3&gt; fred.txt</p> <p>A positional parameter cannot be found that accepts argument &#39;3&gt;&#39;.</p> <p> &nbsp; &nbsp;+ CategoryInfo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: InvalidArgument: (:) [Get-MailPublicFolder], ParameterBindingException</p> <p> &nbsp; &nbsp;+ FullyQualifiedErrorId : PositionalParameterNotFound,Get-MailPublicFolder</p> <p> &nbsp; &nbsp;+ PSComputerName &nbsp; &nbsp; &nbsp; &nbsp;: kenny-exch2010.cartgolf.org</p> <p>Or</p> <p>[PS] C:\Users\Administrator\Documents\WindowsPowerShell&gt;get-mailpublicfolder &#39;\710003 - ABC, Test&#39; | fl alias 3&gt; fred.txt</p> <p>fl : A positional parameter cannot be found that accepts argument &#39;3&gt;&#39;.</p> <p>At line:1 char:48</p> <p>+ get-mailpublicfolder &#39;\710003 - ABC, Test&#39; | fl &lt;&lt;&lt;&lt; &nbsp;alias 3&gt; fred.txt</p> <p> &nbsp; &nbsp;+ CategoryInfo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: InvalidArgument: (:) [Format-List], ParameterBindingException</p> <p> &nbsp; &nbsp;+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.FormatListCommand</p>

  • <p>I&#39;m hitting the same error as well:</p> <p>PS C:\&gt; Write-Warning &quot;this is warning&quot; 3&gt; foo</p> <p>Write-Warning : A positional parameter cannot be found that accepts argument &#39;3&gt;&#39;.</p> <p>At line:1 char:14</p> <p>+ Write-Warning &lt;&lt;&lt;&lt; &nbsp;&quot;this is warning&quot; 3&gt; foo</p> <p> &nbsp; &nbsp;+ CategoryInfo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: InvalidArgument: (:) [Write-Warning], ParameterBindingException</p> <p> &nbsp; &nbsp;+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteWarningCommand</p>

  • <p>@ALL</p> <p>Just a small note. &nbsp;This only works in PowerShell V3 or later.</p>