Powershell Logging

Posted on 2011-10-02
Last Modified: 2012-06-27
Hi Guys/Gals,

I have question how would I go about outputing to log file errors in a csv format. For example errorid, message, application etc.

I know to write to log file in general you would use thing something like:

Start-Transcript -path $LogFile -force
write-host "Error has occured"

How would one utilize the output of the write-host command so it output a message like:
0012 - Tab - Error has occured - Tab - Explorer.exe
The column name being on top of the csv file.

Any help would be much appreciated.


Question by:hellworld12345
    LVL 9

    Expert Comment

    Hmmm, there are a myriad different ways of logging errors in PowerShell.

    Sounds like you want to export into a CSV format though, tell me, can you write out the log file at the end of processing your script, or do you need to write out the log file on the fly?

    If you can wait to the end then you could pipe your errors into an Array object and then export that to CSV afterwards.

    If you need to write out a tab delimited file on the fly you could try this, I am assuming your error messages are in made up variables:

    "$Error" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\Some Folder\Some File.txt" -Append

    This would write out the errors to the file and it should also tab delimit them. The `t character is a tab according to PowerShell.

    Author Comment

    Hi Chrismerritt,

    I would need to write out the log file on the fly. Tried what you said below doesn't seem to be working see screenshot.

    "$Error" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\Some Folder\Some File.txt" -Append

    Thank you.

    LVL 9

    Accepted Solution

    Please try this:

    $ErrorMessage = "error"
    $Message = "Message with spaces"
    $process = "explorer.exe"
    "$ErrorMessage" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\TEMP\file.txt" -Append

    Open in new window


    Author Closing Comment

    Thank you for your help
    LVL 16

    Expert Comment

    by:Dale Harris
    I just want to point out an easier way to write the 5th line:

    "$ErrorMessage" + "`t" + "$Message" + "`t" + "$Process"
    can be written as:

    The thing to remember is use double quotes to allow for powershell to figure out what you're trying to say.  Also the backtick (`) is a special command to Powershell to escape one character forward.  If you put a $ in there or whatever other character that's not special, it will read it as a literal dollar sign, not a variable.  If you put it at the end of a line, it will be continued on the next line.  If you put a n or t after it, it will make a new line or a tab.  Those are some of the ways you can use an escape character.  You no longer have to separate it like VBScript.

    "Hello " & Name & ".  How are you?"


    LVL 9

    Expert Comment

    Whilst it can be written as that yes, I deliberately seperated the content keeping the `t's seperate, otherwise it all kind of merges into one :P
    LVL 16

    Expert Comment

    by:Dale Harris
    Oh okay, sorry to intrude.  You're right, it does keep it easy to read.  Sometimes I overlook readability for less typing, which is not always great :)  Keep up the good work!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now