简体   繁体   中英

Getting a list of timestamps of all files in a folder and its subfolder in PowerShell in Windows 10

I would like to get file tree in text format with all three exact timestamps of each object. I didn't find any utility which offers file tree with exact timestamps, so I decided to use PowerShell. Here is a piece of code which I have so far:

        $items = @()

    dir -Force | foreach { 
        $Name     = $_.Name            
        $Creation = $_.CreationTimeUtc    
        $Modified = $_.LastWriteTimeUtc   
        $Accessed = $_.LastAccessTimeUtc  
        $Size     = $_.Length

        $i = New-Object -TypeName psobject
        $i | Add-Member -MemberType NoteProperty -Name Name               -Value $Name
        $i | Add-Member -MemberType NoteProperty -Name CreatedDateUtc     -Value $Creation
        $i | Add-Member -MemberType NoteProperty -Name CreatedTimeUtc     -Value $Creation.TimeOfDay
        $i | Add-Member -MemberType NoteProperty -Name ModifiedDateUtc    -Value $Modified 
        $i | Add-Member -MemberType NoteProperty -Name ModifiedTimeUtc    -Value $Modified.TimeOfDay
        $i | Add-Member -MemberType NoteProperty -Name AccessedDateUtc    -Value $Accessed 
        $i | Add-Member -MemberType NoteProperty -Name AccessedTimeUtc    -Value $Accessed.TimeOfDay
        $i | Add-Member -MemberType NoteProperty -Name Size               -Value $Size
        $items += $i
        }

        $items | Format-Table

This code is far from perfect because it doesn't show so many file attributes and because it doesn't show subfolders. I couldn't include more file attributes because width of table is limited in PowerShell.

What I would like is modifying this code so that it includes subdirectories, more file properties and saves result in form of CSV file. Can this be done? And if so, how exactly? Thank you very much.

it doesn't show subfolders

To include subfolders, use the -Recurse switch with Get-ChildItem (alias dir ):

Get-ChildItem -Recurse | ...

To save the results to CSV, use the Export-Csv cmdlet:

Get-ChildItem -Recurse |Export-Csv C:\path\to\new\file.csv -NoTypeInformation

If you want specific properties, use the Select-Object cmdlet before piping to Export-Csv :

Get-ChildItem -Recurse |Select-Object Name,CreationTimeUtc |Export-Csv C:\path\to\new\file.csv -NoTypeInformation

If you want the date formatted with more specific time information, put it in a calculated property:

Get-ChildItem -Recurse |Select-Object Name,@{Name='CreatedTimeUtc';Expression={$_.CreationTimeUtc.TimeOfDay}} |Export-Csv C:\path\to\new\file.csv -NoTypeInformation

You could also use a custom date format string:

Get-ChildItem -Recurse |Select-Object Name,@{Name='CreatedTimeUtc';Expression={'{0:HH:mm:ss.fff}' -f $_.CreationTimeUtc}} |Export-Csv C:\path\to\new\file.csv -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM