Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell, can't get LastWriteTime

Tags:

powershell

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

like image 929
JohnL1953 Avatar asked Mar 01 '23 09:03

JohnL1953


1 Answers

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.

like image 110
mklement0 Avatar answered Mar 08 '23 10:03

mklement0