I am writing a script to test for old files in a synchronized folder, and send a text message if a file is older than 2 hours (this would mean that a file failed to update). I haven't included the "texting" part, because that works fine.
When I run my script I get three date:times for many of the files. Why do I get three values for the "last" writetime? And, how do I only look at the most recent "last" writetime?
Here's my script...
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
Here's my output...
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Monday, November 13, 2017 8:01:34 PM
Monday, November 13, 2017 7:45:45 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Monday, November 13, 2017 8:03:56 PM
Monday, November 13, 2017 8:41:05 PM
TX013.7Z
Monday, November 13, 2017 8:01:33 PM
Monday, November 13, 2017 7:33:20 PM
Friday, November 10, 2017 2:38:20 PM
TX050.7z
Monday, November 13, 2017 8:02:17 PM
Monday, November 13, 2017 9:46:19 AM
Tuesday, October 31, 2017 7:46:01 AM
Help would be greatly appreciated!!
I ran the code that Stephen suggested. I am still getting multiple values for LastWriteTime for many of my files.
This is the resultant output on 11/24/17 at 8:57 AM, when all files show a timestamp in a dir of approximately 8:46 AM or so.
PS C:\Windows\system32> $Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
TX025.7Z
Thursday, November 23, 2017 11:04:20 AM
Friday, November 24, 2017 8:46:54 AM
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Thursday, November 23, 2017 11:01:26 AM
Friday, November 24, 2017 8:45:45 AM
TX049.7Z
Thursday, November 23, 2017 11:03:18 AM
Wednesday, November 22, 2017 5:46:02 PM
GA007.7Z
Thursday, November 23, 2017 7:45:38 PM
Thursday, November 23, 2017 11:02:20 AM
GA004.7Z
Friday, November 24, 2017 8:47:01 AM
Thursday, November 23, 2017 11:30:53 AM
GA003.7Z
Friday, November 24, 2017 8:47:10 AM
Thursday, November 23, 2017 10:30:48 AM
MO001.7Z
Thursday, November 23, 2017 11:04:45 AM
Thursday, November 23, 2017 7:47:08 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Thursday, November 23, 2017 11:03:48 AM
Friday, November 24, 2017 8:41:02 AM
AR001.7Z
Thursday, November 23, 2017 7:46:02 PM
Thursday, November 23, 2017 11:05:08 AM
TX013.7Z
Thursday, November 23, 2017 11:01:25 AM
Friday, November 24, 2017 8:34:11 AM
TX050.7z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:46:19 AM
TX024.7Z
Thursday, November 23, 2017 11:03:48 AM
Thursday, November 23, 2017 8:40:36 PM
GA010.7Z
Friday, November 24, 2017 8:45:24 AM
Thursday, November 23, 2017 11:02:39 AM
TX002.7Z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:43:45 AM
TX035.7Z
Thursday, November 23, 2017 11:03:17 AM
Friday, November 24, 2017 8:51:39 AM
PS C:\Windows\system32>
You are overly complicating the process. You can simply add a select-object to grab the 2 properties you want.
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#Filter for *.7z, with a lastwritetime older than 2 hours
#Grab only the name and lastwritetime
Get-Childitem $FPath -Filter "*.7z" |
Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) } |
Select-Object Name, LastWriteTime
}
EDIT: Explaining the logic of the original process
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
Without seeing the original file structure its hard to tell exactly what you are working with but the above should explain why you are getting multiple lastwritetime values in your script.
Related
Importing a CSV file that has date/time values a column "Expiration" as "MM/DD/YY, HH:MM AM" (or PM). When I parse through the file I store it in an object of type System.Collections.ArrayList (not sure that matters) and I'd like to export the results in descending date/time order. When I use:
Sort-Object -Property Expiration -Descending
It sorts the results mostly in order but by the 1st integer of the DD portion so it looks like this:
2/1/21, 3:54 AM
2/11/21, 7:59 AM
2/2/21, 4:44 AM
2/21/21, 6:24 AM
2/3/21, 3:58 AM
2/4/21, 3:59 AM
What can I do to get this sorted properly upon export? I also tried sorting A-Z in Excel upon output but it does the same thing.
That is because these are strings, not real DateTime objects. You need to make them DateTime objects.
By default, this...
2/1/21, 3:54 AM
2/11/21, 7:59 AM
2/2/21, 4:44 AM
2/21/21, 6:24 AM
2/3/21, 3:58 AM
2/4/21, 3:59 AM
... on import will be separate columns if no header is specified, and only the date, not time, if the property used. These strings must be properly quoted to be read as one column.
"Expiration"
"2/1/21, 3:54 AM"
"2/11/21, 7:59 AM"
"2/2/21, 4:44 AM"
"2/21/21, 6:24 AM"
"2/3/21, 3:58 AM"
"2/4/21, 3:59 AM"
Example - Date actions
Sort-Object { $PSitem.Expiration -as [datetime] }
# Or these
$sortedDates = $dates |
Sort-Object {[System.DateTime]::ParseExact($PSItem, "MM/dd/yyyy", $null)}
# Or DateTIme parsing/formatting like these
[DateTime]"2020-7-16"
[DateTime]"Jul-16"
'{0:yyyy-MM-dd}' -f [DateTime]'Jul-16'
# Or
[datetime]::parseexact($PSitem.Expiration, 'dd-MMM-yy', $null)
# Output DateTime
[datetime]::parseexact($PSitem.Expiration, 'dd-MMM-yy', $null).ToString('yyyy-MM-dd')
# Or accept the default of ParseExect.
Import-CSV -Path 'C:\Scripts\Dates.csv' |
ForEach {[DateTime]::Parse($PSitem.Expiration)} |
Sort-Object
# Results
<#
Monday, February 1, 2021 3:54:00 AM
Tuesday, February 2, 2021 4:44:00 AM
Wednesday, February 3, 2021 3:58:00 AM
Thursday, February 4, 2021 3:59:00 AM
Thursday, February 11, 2021 7:59:00 AM
Sunday, February 21, 2021 6:24:00 AM
#>
Import-CSV -Path 'C:\Scripts\Dates.csv' |
ForEach {[DateTime]::Parse($PSitem.Expiration)} |
Sort-Object -Descending
# Results
<#
Sunday, February 21, 2021 6:24:00 AM
Thursday, February 11, 2021 7:59:00 AM
Thursday, February 4, 2021 3:59:00 AM
Wednesday, February 3, 2021 3:58:00 AM
Tuesday, February 2, 2021 4:44:00 AM
Monday, February 1, 2021 3:54:00 AM
#>
See also: PowerTip: Use PowerShell to Format Dates
I Have a variable stored with this value
PS C:\Users\> $Time
Monday, November 30, 2020 8:55:01 AM
Sunday, October 18, 2020 11:10:01 PM
Sunday, November 8, 2020 10:40:34 PM
Sunday, November 8, 2020 11:47:37 PM
Sunday, November 8, 2020 10:59:57 PM
Tuesday, December 1, 2020 3:15:42 AM
Monday, November 30, 2020 7:00:32 PM
Monday, November 30, 2020 12:19:06 AM
Monday, November 30, 2020 7:01:34 PM
Tuesday, December 1, 2020 1:12:10 AM
Tuesday, December 1, 2020 2:37:18 AM
Sunday, November 1, 2020 7:39:34 PM
Sunday, September 27, 2020 11:48:38 PM
I want the time formats of the variable $time to change to "yyyy-mm-dd hh:mm:ss" so that ALL of the list is displayed
PS C:\Users\> $Time
2020-11-30 08:55:01
2020-10-18 11:10:01
2020-11-08 10:40:34
2020-11-08 11:47:37
2020-11-08 10:59:57
2020-12-01 03:15:42
2020-11-30 07:00:32
2020-11-30 12:19:06
2020-11-30 07:01:34
2020-12-01 01:12:10
2020-12-01 02:37:18
2020-11-01 07:39:34
2020-09-27 11:48:38
Please help me creating a code for the same
thanks
This is a pretty common question that gets asked here on StackOverflow, however, it seems most of the answers are directed towards converting a variable which stores a single date, to a formatted string.
Whereas you have an array of dates you want to convert.
I'm going to make the assumption that you have an Array of DateTime values, and not an Array of String.
For starters, there's TONS of blogs and articles about this, not to mention the documentation.
https://devblogs.microsoft.com/scripting/formatting-date-strings-with-powershell/
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date
Depending on how you need to use this data there is a million different ways to do this.
Primarily, you need to learn how to perform actions against an array of objects. Using things like ForEach, ForEach-Object, Select-Object, etc. Once you learn how to use those, then the problem just becomes "how do you format a date to a string", which is all over the place on here and the rest of the internet.
Here's some examples:
# Use this to generate sample data:
$Time = 10000,9000,8000,7000,6000,5000,4000,3000,2000,1000 |
ForEach-Object { (Get-Date).AddMinutes(-$_) }
## Various solutions:
$Time | ForEach-Object { $_.ToString('yyyy-MM-dd HH:mm:ss') }
$Time | ForEach-Object { $_ | Get-Date -Format 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { $_ | Get-Date -f 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { Get-Date $_ -f 'yyyy-MM-dd HH:mm:ss' }
$Time | Select-Object #{N='TimeString'; E={$_.ToString('yyyy-MM-dd HH:mm:ss')}}
foreach ($tv in $time) { $tv.ToSTring('yyyy-MM-dd HH:mm:ss') }
$Time.ForEach({$_.ToString('yyyy-MM-dd HH:mm:ss')})
# Other methods submitted in comments, thanks #iRon
$Time | ForEach-Object ToString('yyyy-MM-dd HH:mm:ss')
$Time.ForEach('ToString', 'yyyy-MM-dd HH:mm:ss')
Note that this is case sensitive.
MM - Means the two digit month value
mm - Means the two digit day value
I have lots of file scanned PDF documents that have the file named with an included date. For example:
FileA_2017-10-15.pdf
FileB_2016-04-08.pdf
FileC_2018-01-30.pdf
some files also are formatted with an underscore at the end as well such as...
FileD_2018-01-30_1.pdf
FileE_2018-01-30_2.pdf
there are even a few that have two underscores before the date such as...
FileF_Example_2018-01-30_1.pdf
FileG_Example_2018-01-30_2.pdf
Unfortunately, the date they were scanned in is different than the actual date of the document. So the "Date Created" and "Date Modified" attributes are different than what is shown in the file name.
I would like a script that I could run to change the "Date Created" and "Date Modified" to match that of the filename.
I attempted this using someone else's script but I don't know enough about PowerShell to make it actually work. Note that I do not want to change the name of the file, only the time stamp.
$Directory = "C:\TestFolder"
$DateFormat = "yyyy-MM-dd"
foreach ($file in (Get-ChildItem $Directory)) {
$date_from_file=GetFileName::[datetime])
$file.CreationTime = $date_from_file
$file.LastAccessTime = $date_from_file
$file.LastWriteTime = $date_from_file
Write-Host ($file.Name + " - " + $date_from_file)
}
The code above can be scraped if something else has already been written since what I have doesn't work.
Edit
Wondering if it would also be possible to add to the script so that it could include files in sub-folders as well. Maybe it could be scripted in a way that would only consider the files in a folder if the Date Modified on the folder is today. I would like to run this on a parent folder that could potentially have many sub-folders and if those folders don't have a "Date Modified" of today, then it should skip the files in that folder. I was thinking that could speed up the process. Open to thoughts and thanks for the help!
You are quite near, you need
split the date part from filename and convert it to a [datetime]
I use a RegEx with a capture group anchored at the end $ of the BaseName
## Q:\Test\2019\05\19\SO_56211626.ps1
$Directory = "C:\TestFolder"
foreach ($file in (Get-ChildItem -Path $Directory -Filter *.pdf)){
if($File.BaseName -match '_(\d{4}-\d{2}-\d{2})(_\d)?$'){
$date_from_file= (Get-Date $Matches[1])
$file.CreationTime = $date_from_file
$file.LastAccessTime = $date_from_file
$file.LastWriteTime = $date_from_file
$file | Select-Object Name,CreationTime,LastAccessTime,LastWriteTime
}
}
Sample output:
> Q:\Test\2019\05\19\SO_56211626.ps1
Name CreationTime LastAccessTime LastWriteTime
---- ------------ -------------- -------------
FileA_2017-10-15.pdf 2017-10-15 00:00:00 2017-10-15 00:00:00 2017-10-15 00:00:00
FileB_2016-04-08.pdf 2016-04-08 00:00:00 2016-04-08 00:00:00 2016-04-08 00:00:00
FileC_2018-01-30.pdf 2018-01-30 00:00:00 2018-01-30 00:00:00 2018-01-30 00:00:00
An English locale (en-US) produces:
Name CreationTime LastAccessTime LastWriteTime
---- ------------ -------------- -------------
FileA_2017-10-15.pdf 10/15/2017 12:00:00 AM 10/15/2017 12:00:00 AM 10/15/2017 12:00:00 AM
FileB_2016-04-08.pdf 4/8/2016 12:00:00 AM 4/8/2016 12:00:00 AM 4/8/2016 12:00:00 AM
FileC_2018-01-30.pdf 1/30/2018 12:00:00 AM 1/30/2018 12:00:00 AM 1/30/2018 12:00:00 AM
[
edit - since the OP is getting very strange errors with my suggested fix - errors that i cannot reproduce with the sample data - i've changed this answer to the full suggested code.
edit 2 - added new file name variants and code to deal with them.
edit 3 - changed from splitting to a regex match since the sample data has changed yet again. [*sigh ...*]
]
you are not actually creating the datetime object that you need. the $date_from_file= line doesn't actually do anything other than create red error msgs ... [grin]
replace this line ...
$date_from_file=GetFileName::[datetime])
... with this line ...
$date_from_file = [datetime]::ParseExact($File.BaseName.Split('_')[-1], $DateFormat, $Null)
... and your $date_from_file variable will contain a proper [datetime] object that will work in your assignments.
i would likely change the sequence of those assignments to put the $file.LastAccessTime = $date_from_file LAST so that it doesn't get changed by the next line.
also, that value will change any time that the file is accessed, so it may not be worth changing. [grin]
here is the full script along with what it does -
what it does ...
sets the location & the date format to use
creates a set of test files from the OPs sample file names
gets the files from the source
converts the .BaseName into a [datetime] object
assigns the .CreationTime, .LastWriteTime, & .LastAccessTime values to the datetime from the file name
displays the changed values
here is the code ...
$Directory = $env:TEMP
$DateFormat = "yyyy-MM-dd"
# create some test files
$TestFileList = #(
'FileA_2017-10-15.pdf'
'FileB_2016-04-08.pdf'
'FileC_2018-01-30.pdf'
'FileD_2019-09-09_1.pdf'
'FileE_2015-05-05_2.pdf'
)
foreach ($TFL_Item in $TestFileList)
{
$Null = New-Item -Path $Directory -Name $TFL_Item -ItemType File -Force
}
$FileList = Get-ChildItem -LiteralPath $Directory -Filter '*.pdf' -File
foreach ($FL_Item in $FileList) {
# removed split, added regex match to work with ever-growing list of variant file names
$Null = $FL_Item.BaseName -match '_(?<DateString>\d{4}-\d{2}-\d{2})'
$DateString = $Matches.DateString
$date_from_file = [datetime]::ParseExact($DateString, $DateFormat, $Null)
$FL_Item.CreationTime = $date_from_file
$FL_Item.LastWriteTime = $date_from_file
$FL_Item.LastAccessTime = $date_from_file
# show the resulting datetime info
'=' * 20
$CurrentFileInfo = Get-Item -LiteralPath $FL_Item.FullName
$CurrentFileInfo.FullName
$CurrentFileInfo.CreationTime
$CurrentFileInfo.LastWriteTime
$CurrentFileInfo.LastAccessTime
}
screen output ...
====================
C:\Temp\FileA_2017-10-15.pdf
2017 October 15, Sunday 12:00:00 AM
2017 October 15, Sunday 12:00:00 AM
2017 October 15, Sunday 12:00:00 AM
====================
C:\Temp\FileB_2016-04-08.pdf
2016 April 08, Friday 12:00:00 AM
2016 April 08, Friday 12:00:00 AM
2016 April 08, Friday 12:00:00 AM
====================
C:\Temp\FileC_2018-01-30.pdf
2018 January 30, Tuesday 12:00:00 AM
2018 January 30, Tuesday 12:00:00 AM
2018 January 30, Tuesday 12:00:00 AM
====================
C:\Temp\FileD_2019-09-09_1.pdf
2019 September 09, Monday 12:00:00 AM
2019 September 09, Monday 12:00:00 AM
2019 September 09, Monday 12:00:00 AM
====================
C:\Temp\FileE_2015-05-05_2.pdf
2015 May 05, Tuesday 12:00:00 AM
2015 May 05, Tuesday 12:00:00 AM
2015 May 05, Tuesday 12:00:00 AM
i checked the files directly in explorer & they match the displayed values.
Thanks. I was stuck without this thread. I ended up with a variation that matched any filename with a correctly formatted date, thus:
# Call like:
# powershell -NoLogo -ExecutionPolicy Unrestricted -Sta -NonInteractive -WindowStyle Normal -File ".\Rename_files_selected_folders_ModifyDateStamps.ps1" -Folder "T:\files"
# 1. capture a commandline parameter 1 as a mandatory "Folder string" with a default value
param ( [Parameter(Mandatory=$true)] [string]$Folder = "T:\HDTV\autoTVS-mpg\Converted" )
[console]::BufferWidth = 512
$DateFormat = "yyyy-MM-dd"
write-output "Processing Folder: ",$Folder
# 2. Iterate the files
$FileList = Get-ChildItem -Recurse $Folder -Include '*.mp4','*.bprj','*.ts' -File
foreach ($FL_Item in $FileList) {
$ixxx = $FL_Item.BaseName -match '(?<DateString>\d{4}-\d{2}-\d{2})'
if($ixxx){
#write-output $FL_Item.FullName
$DateString = $Matches.DateString
$date_from_file = [datetime]::ParseExact($DateString, $DateFormat, $Null)
$FL_Item.CreationTime = $date_from_file
$FL_Item.LastWriteTime = $date_from_file
$FL_Item | Select-Object FullName,CreationTime,LastWriteTime
}
}
# https://stackoverflow.com/questions/56211626/powershell-change-file-date-created-and-date-modified-based-on-filename
How do I specify what part of my input string is the date and month?
If the input is 01/10/2017, this can be read as 1st Oct 2017 and 10th Jan 2017. Both are correct.
I want to be explicit that 01 is date and 10 is month, so that irrespective of locale and time format I can get a consistent result.
Sample code:
get-date -Date '01/10/2017'
The output is:
Tuesday, January 10, 2017 12:00:00 AM
The desired output is:
Sunday, October 01, 2017 12:00:00 AM
I have a solution for you. It requires that the culture as one of the arguments.
([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('en-GB')))
A culture does not have to be specified. However, the argument for it does, otherwise you will get an error:
Cannot find an overload for "ParseExact" and the argument count: "2".
[cultureinfo]::InvariantCulture or $null can be used as the third argument:
$date = "01/10/2017"
[datetime]::ParseExact($date, "dd/MM/yyyy", [cultureinfo]::InvariantCulture)
[datetime]::ParseExact($date, "dd/MM/yyyy", $null)
Output in all three cases
01 October 2017 00:00:00
Try this:
Get-Date(Get-Date -Date $date -Format 'dd/MM/yyyy')
You can enforce the culture for single commands (or command blocks). This should help avoiding that date chaos.
PS C:\> [System.Threading.Thread]::CurrentThread.CurrentUICulture = "en-US" ; [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"; get-date -Date '01/10/2017'
Tuesday, January 10, 2017 12:00:00 AM
PS C:\> [System.Threading.Thread]::CurrentThread.CurrentUICulture = "en-GB" ; [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-GB"; get-date -Date '01/10/2017'
01 October 2017 00:00:00
My select string looks like:
$prevdate = Get-Content "C:\Prevday.txt"
Select-String -Path "C:\latestdatetime.txt" -Pattern "$prevdate" | Measure-Latest | Out-File "C:\Prevday_snap.txt"
But the output which I am getting displayed is
C:\latestdatetime.txt:1:8/17/2015 9:18:45 AM
I don't want the part which has been styled as Bold. How can I get this done.?
Select-String returns MatchInfo objects. The text of the matched line will be in the Line property of those objects.
Select-String -Path "C:\latestdatetime.txt" -Pattern "$prevdate" | Select -ExpandProperty Line
I am not sure I understand the question clearly but it sounds like you would like the value 'part' of select-string result, and not where it is found. If so, you need to go into the properties of the match object you get from select-string. For example:
PS C:\Users\Adil>
gc C:\Temp\a.txt
$date = "10:34:31 AM"
select-string -path C:\temp\a.txt -Pattern $date | % {$_.matches.groups.value}
Tuesday, August 18, 2015 10:32:58 AM
Tuesday, August 17, 2015 10:34:31 AM
Tuesday, August 16, 2015 10:34:32 AM
Tuesday, August 18, 2015 10:34:31 PM
Tuesday, August 18, 2015 11:34:31 AM
Tuesday, August 18, 2015 12:34:31 AM
Tuesday, August 18, 2015 13:34:31 AM
10:34:31 AM