Thursday, January 19, 2017

Write output to the file in PowerShell

When we create PowerShell scripts we often add some useful info to the trace using Write-Host method:

   1: Write-Host "Check web" $web.Url

It will write trace to console. How to write output to the file from PowerShell? One way is of course use .Net System.IO.File class, but in PowerShell there is also more convenient native syntax:

   1: ("Check web" + $web.Url) | Out-File "log.txt" -Append

As result it will write the same string which we used in example with Write-Host to the log.txt. The difference with this syntax and Write-Host is that in last case you need to explicitly concatenate strings using + operator, while in Write-Host it is not necessary.

1 comment:

  1. Thank you for this great article which conveyed a good information.keep more updates.
    SEO Companies in India

    ReplyDelete