Formatting variable to Date

I have a log file that I am parsing for last successful run date. I was able to parse that out of the log file…with this command

Get-Content -Path 'C:***********.messages' -Tail 75 | Select-String -Pattern 'Finished: 1 of 1' | FL | Out-String

The output of the above is what I want but the log file has the date all weird in the format
Mon Aug 1 20:30:50 2022

I am trying to figure out how to take the above output and format it like below
Mon Aug 1 2022 20:30:50

I have been trying to search google for everything i can think might work and none of it seems to get me where I want to be…So I am here hoping someone has for more knowledge than me on this.

If the format is always the same the easiest way would be to split the string on the spaces and re-assemble it in the desired order like this:

('Mon Aug 1 20:30:50 2022' -split ' ')[0,1,2,4,3] -join ' '

The Output would be …

Mon Aug 1 2022 20:30:50

The other option would be to parse the string to a [DateTime] type. Then you can format to any of the available options for this type. Depending on your actual language setting you may adjust the language code in this snippet.

[datetime]::ParseExact('Mon Aug 1 20:30:50 2022','ddd MMM d HH:mm:ss yyyy',[Globalization.CultureInfo]::CreateSpecificCulture('en-us'))

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thank you for the response, sorry about the incorrect format…Last time I was here it was backticks. The button will make things a lot easier.
You are a genius the first option works perfectly. Now the format is closer to the rest of the data collected and i can improve a few other options I have created in the past. Thank you for educating me.

I’m glad it worked. :+1:t4: :slightly_smiling_face:

It still is - but that’s more for single cmdlets names or parameters inside the plain text. If you have a code snippet it looks better with the button. :wink:

sometimes I really hate that everyone formats logs their own way…
I have run into small issue. Some of the dates are not formatting correctly. I found out that there is a difference in log format based on the day. Not all days are double digit. So when the day is 1-9 the format below doesnt work because of the extra space. I tried to split each character to its own line…and the join them so that i can always get the same date format…it works but it looks like a jumbled mess.

$log = Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Content -Path 'C:**************.messages' -Tail 75 | Select-String -Pattern 'Finished: 1 of 1 hosts' | FL | Out-String}
$date = $log.Split("[]") | Select-Object -First 2 | Select-Object -Last 1
($date -split ' ')[0,1,2,4,3] -join ' '
$log = Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Content -Path 'C:**************.messages' -Tail 75 | Select-String -Pattern 'Finished: 1 of 1 hosts' | FL | Out-String}
$date = $log.Split("[]") | Select-Object -First 2 | Select-Object -Last 1
($date -split '')[1,2,3,5,6,7,9,10,21,22,23,24,12,13,14,15,16,17,18,19] -join ''

Output for $log looks like this:

[LINE:][Wed Aug  3 22:39:57 2022][4720.0][scan=][duration=] : Finished: 1 of 1 hosts

Any thoughts on how i can clean it up and make it look better?

You can use regex patterns for your -split operator. So

($date -split "\s+")[0,1,2,4,3] -join ' '

should do the trick. :wink:

Another option would be to replace double spaces with single spaces beforehand.

$date -replace '  ', ' '

… or … .even better … to replace all occurences of more than one space with single space:

$date -replace "\s+", ' '
1 Like