Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

LastAccessTime always returns the same value

$
0
0

I have what I believe to be a straightforward command to list files and the times associated with them with the command below:

 

$FileList = Get-ChildItem  -Path $DrivePath -Recurse -Include *.png,*.xlsx `
        | Select-Object Name, Length, DirectoryName, IsReadOnly, LastAccessTime, LastWriteTime
    foreach ($File in $FileList) 
    {
        $MyDate = get-date -format g;
        $Out = New-Object PSObject
        $Out | Add-Member NoteProperty Name              $File.Name;
        $Out | Add-Member NoteProperty Length            $File.Length;
        $Out | Add-Member NoteProperty DirectoryName     $File.DirectoryName;
        $Out | Add-Member NoteProperty IsReadOnly        $File.IsReadOnly;
        $Out | Add-Member NoteProperty LastAccessTime    $File.LastAccessTime;
        $Out | Add-Member NoteProperty LastWriteTime     $File.LastWriteTime;
        $Out | Add-Member NoteProperty DateGathered      $MyDate;
        #Write-Output "now working on $File.FullName";
        $OutArray += $Out
    }  # foreach ($File in $FileList)

    $OutArray | export-csv $DestinationFile -NoTypeInformation;

If I do a straight output to the screen, the LastAccessTime is okay. 
However, the LastAccessTime always seems to return 5/12/2016  8:42:50 AM in the output directory when I use "foreach ($File in $FileList)", then the change seems to happen. Not only that but the properties of the file have LastAccessTime also set to 5/12/2016  8:42:50 AM !

If the true LastAccessTime is more recent than that, it will output the more recent time, but my attempt is to only read LastAccessTime, not modify it.

Any ideas why the setting is being changed?


Viewing all articles
Browse latest Browse all 6937

Trending Articles