I have this working, but need LastWriteTime
and can't get it.
Get-ChildItem -Recurse | Select-String -Pattern "CYCLE" | Select-Object Path, Line, LastWriteTime
I get an empty column and zero Date-Time data
Select-String
's output objects, which are of type Microsoft.PowerShell.Commands.MatchInfo
, only contain the input file path (string), no other metadata such as LastWriteTime
.
To obtain it, use a calculated property, combined with the common -PipelineVariable
parameter,
which allows you to reference the input file at hand in the calculated property's expression script block as a System.IO.FileInfo
instance as output by Get-ChildItem
, whose .LastWriteTime
property value you can return:
Get-ChildItem -File -Recurse -PipelineVariable file |
Select-String -Pattern "CYCLE" |
Select-Object Path,
Line,
@{
Name='LastWriteTime';
Expression={ $file.LastWriteTime }
}
Note how the pipeline variable, $file
, must be passed without the leading $
(i.e. as file
) as the -PipelineVariable
argument . -PipelineVariable
can be abbreviated to -pv
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With