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

**URL:** <https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538>\
**Category:** PowerShell Help\
**Created:** [June 8, 2022, 1:08pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538 "2022-06-08T13:08:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forums.powershell.org/u/Ben)\
**Post date:** [June 8, 2022, 1:08pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538/1 "2022-06-08T13:08:21Z")

</div>

```auto

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
        }
      }

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 8, 2022, 3:04pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538/2 "2022-06-08T15:04:49Z")

</div>

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`. 😉

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')`😉

I’d do it like this:

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forums.powershell.org/u/Ben)\
**Post date:** [June 8, 2022, 3:30pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538/3 "2022-06-08T15:30:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [June 8, 2022, 4:26pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538/4 "2022-06-08T16:26:14Z")

</div>

Olaf is god-like, for sure. 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/i-need-get-file-and-last-write-time-export-csv-file-but-time-is-an-object-how-to-set-to-get-time-please-help/19538/5 "2024-05-16T20:17:54Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


