I'm trying to find files in a directory which contain a string. I'm okay with that part, but I can't figure out how to get the CreationTime for the file containing the string.
This gives me what I need except for CreationTime.
Get-ChildItem |
Select-String -Pattern 'GREEN' |
select-object -Property Filename, Line, LineNumber
Trying foreach but can't get at the file CreationTime.
$collection = Get-ChildItem
foreach ($currentItemName in $collection) {
if ( $currentItemName | Select-String -Pattern 'GREEN') {
$currentItemName |Select-String -Pattern 'GREEN' | `your text`
Select-Object -Property Filename, Line, LineNumber;
$currentItemName | Select-Object -Property CreationTime;
}
}
My lack of understanding regarding object type is part of my problem. The objects in Get-ChildItem includes time details, but the object type after Select-String does not.
You can use
Select-Objectto construct new properties on-the-fly by using a so-called calculated property expression:Alternatively use nested loops to keep track of the file object piped to
Select-Stringand then construct your own custom output objects by combining it with the selection results: