none
Formating the result of select-object

    Question

  • Hi,

    I have a command that returns information about the event log :

    Get-EventLog -LogName System -After $after -Before $before | select-object Source,TimeGenerated,MachineName,UserName,Message | where-object {$_.Source -eq "Print"}

    This works fine so far but I now need to format the TimeGenerated field with a different date format. Any ideas ?

    Thanks

    Thursday, March 15, 2012 1:50 PM

Answers

  • You don't specify a format, so this is just an example:

    Get-EventLog -LogName System -After $after -Before $before | `
        select-object Source,@{n='TimeGenerated';e={Get-Date ($_.timegenerated) -Format 'yyyyMMdd'}},MachineName,UserName,Message | `
        where-object {$_.Source -eq "Print"}


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Proposed as answer by Shabarinath Thursday, March 15, 2012 2:16 PM
    • Marked as answer by benkunz Thursday, March 15, 2012 2:23 PM
    Thursday, March 15, 2012 2:14 PM

All replies

  • You don't specify a format, so this is just an example:

    Get-EventLog -LogName System -After $after -Before $before | `
        select-object Source,@{n='TimeGenerated';e={Get-Date ($_.timegenerated) -Format 'yyyyMMdd'}},MachineName,UserName,Message | `
        where-object {$_.Source -eq "Print"}


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Proposed as answer by Shabarinath Thursday, March 15, 2012 2:16 PM
    • Marked as answer by benkunz Thursday, March 15, 2012 2:23 PM
    Thursday, March 15, 2012 2:14 PM
  • perfect, thanks
    Thursday, March 15, 2012 2:22 PM
  • instaed of

    Get-Date ($_.timegenerated) -Format 'yyyyMMdd'

    you can use

    $_.timegenerated.ToString('yyyyMMdd')


    my blog: http://shserg.ru/

    Thursday, March 15, 2012 3:23 PM
  • You don't specify a format, so this is just an example:

    Get-EventLog -LogName System -After $after -Before $before | `
        select-object Source,@{n='TimeGenerated';e={Get-Date ($_.timegenerated) -Format 'yyyyMMdd'}},MachineName,UserName,Message | `
        where-object {$_.Source -eq "Print"}


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Good stuff Grant. Just a minor thing that I would change to fall in line with a best practice:

    You should do all of your filtering as far left in your command if possible and do the output formatting at the very end. Think of it as "Filter Left, Format Right". This also helps because you will have less data to format at the end instead of doing the formatting at the very beginning and then trying to filter it.

    Also, you don't have to use the backtick "`" after the pipe because it serves as a natural line continuation.

    Get-EventLog -LogName System -After $after -Before $before | 
        where-object {$_.Source -eq "Print"} |
            select-object Source,@{n='TimeGenerated';e={Get-Date ($_.timegenerated) -Format 'yyyyMMdd'}},MachineName,UserName,Message


    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

    • Proposed as answer by jrv Thursday, March 15, 2012 3:33 PM
    Thursday, March 15, 2012 3:31 PM