Friday, October 10, 2014

Windows Powershell Script - write to both console and file

Few days back I was writing my first power shell script and thought to write my output to both console and some logfile. Surprisingly it wasn't as simple I thought and I ended up doing this.
function logMsg($msg)
{
    write-output $msg
    write-host $msg 
}
usage in script:
logMsg("My Error Msg")
logMsg("My Info Msg")
powershell script execution call:
ps> .\myFirstScript.ps1 >> testOutputFile.txt

here write-host is to write to console and write-output takes care of writing to my log file.

No comments:

Post a Comment

About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!