All outputs to file
In one of days i was creating migration script and thought about the way how to put error output and all PS messages to one system file. I knew about Write-Host and Write-Output file, but never thought about redirecting all output threads to file.
Solution can vary:
- In powershell 2.0 you need to run script with separate powershell command
- In powershell 3.0 you can use separate streams for each type of information:
powershell myScript.ps1 > $somefile
Don’t try to use start-transcript command. It does not record Write-Error, Write-Verbose, or Write-Debug...only standard output.
("The transcript includes all command that the user types and all output that appears on the console"). “Tee” command doesn’t redirect warning stream.
In PS 3.0, we've extended output redirection to include the following streams:
Pipeline (1)
Error (2)
Warning (3)
Verbose (4)
Debug (5)
All (*)
Some scenarios of usage for PS 3.0 are described here: http://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=297055
Some
links:
- http://stackoverflow.com/questions/7355137/powershell-2-easy-way-to-direct-every-bit-of-output-to-a-file
- http://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution
- http://stackoverflow.com/questions/3008545/powershell-writing-errors-and-ouput-to-a-text-file-and-console
- http://rkeithhill.wordpress.com/2009/03/06/effective-powershell-item-14-capturing-all-output-from-a-script/
Regular expressions
One of point i would like to mention here is case sensitivity. Usually regular expressions are case sensitive by default (even in .NET framework ) . However, it is not true in PowerShell. -match and -replace are case insensitive, as are -imatch and -ireplace.