Formating the result of select-object
-
Thursday, March 15, 2012 1:50 PM
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
All Replies
-
Thursday, March 15, 2012 2:14 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
- 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:22 PMperfect, thanks
-
Thursday, March 15, 2012 3:23 PM
instaed of
Get-Date ($_.timegenerated) -Format 'yyyyMMdd'
you can use
$_.timegenerated.ToString('yyyyMMdd')
my blog: http://shserg.ru/
-
Thursday, March 15, 2012 3:31 PMModerator
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
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,MessageBoe 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 jrvMicrosoft Community Contributor Thursday, March 15, 2012 3:33 PM

