I need get file and last write time, export CSV file, but time is an object, how to set to get time,please help


foreach($volume in Get-Volume){
    if(($null -ne $volume.DriveLetter) -and $volume.FileSystem -eq "NTFS"){
        $drive =($volumes.DriveLetter+":\")
    
        $FolderPath = Get-ChildItem $drive -Recurse -Depth 1  -Force
        # $FolderPath.lastwritetime
        $writetimes = get-childitem 
        $Output = @()
        ForEach ($Folder in $FolderPath) {
            $Acl = Get-Acl -Path $Folder.FullName
            ForEach ($Access in $Acl.Access) {
      
   $Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference; `
   'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited;[datetime]'writetime'=$FolderPath.lastwritetime.ToString()}
        $Output += New-Object -TypeName PSObject -Property $Properties    
               }
            }
            {
                 
        }
        # }
        $Output | Export-Csv -Path C:\1\test.csv
        }
      }

You have at least two typos in your code and your code is very bad formatted and because of that hard to read.

Instead of $volumes.DriveLetter it should be singlular: $volume.DriveLetter in the third line of code you posted.

And for your writetime property it should be $Folder.lastwritetime instead of $FolderPath.lastwritetime. :wink:

Why do you actually cast the property to string when you actually want to have the original type? Regardless of that - the type will not survive the export to CSV anyway. So I’d get rid of the type. Instead you could cast it to a particular string format of your choice. … like .toString('yyyy-MM-dd_HH:mm:ss') :wink:

I’d do it like this:

$DriveLetterList = 
    Get-Volume | 
        Where-Object {
            $null -ne $_.DriveLetter -and
            $_.FileSystem -eq 'NTFS'
        } |
        ForEach-Object {
            '{0}:\' -f $_.DriveLetter
        }

$Output = 
foreach ($drive in $DriveLetterList) {
    $FolderList = 
        Get-ChildItem $drive -Recurse -Depth 1 -Force -ErrorAction SilentlyContinue
    forEach ($Folder in $FolderList) {
        $Acl = Get-Acl -Path $Folder.FullName
        forEach ($Access in $Acl.Access) {
            [PSCustomObject]@{
                FolderName  = $Folder.FullName
                Group_User  = $Access.IdentityReference
                Permissions = $Access.FileSystemRights
                Inherited   = $Access.IsInherited
                writetime   = $Folder.lastwritetime
            }
        }
    }
}
$Output | Format-Table -AutoSize

$Output | Export-Csv -Path C:\1\test.csv

Wow, god, that’s awesome. I think the code I wrote before is terrible. I need to learn more

Olaf is god-like, for sure. :slight_smile: