rena
rena

Reputation: 1278

PowerShell Move-Item Folder Time Stamp

I'm moving some folders and all their content with PowerShell Move-Item, but when I moved the folder its date modified is the current date not the original. How can I move the folder without changing its last modified date.

Upvotes: 0

Views: 2110

Answers (2)

Oz Bar-Shalom
Oz Bar-Shalom

Reputation: 1855

Here's a powershell function that'll do what you're asking... It does absolutely no sanity checking, so caveat emptor...

function Move-FileWithTimestamp {
[cmdletbinding()]
param(
    [Parameter(Mandatory=$true,Position=0)][string]$Path,
    [Parameter(Mandatory=$true,Position=1)][string]$Destination
)

    $origLastAccessTime = ( Get-Item $Path ).LastAccessTime
    $fileName = ( Get-Item $Path ).Name
    Move-Item -Path $Path -Destination $Destination
    $(Get-Item ($Destination+'\'+$fileName)).LastAccessTime = $origLastAccessTime
}

Once you've run loaded that, you can do something like:

Move-FileWithTimestamp c:\foo.txt c:\newFolder

Upvotes: 0

Svperstar
Svperstar

Reputation: 497

There's no simple way to do this in windows but you can use robocopy. There is an option to copy the modified date attribute. Run help on robocopy (robocopy /?) and look for /COPY and /COPYALL options. This only works in Vista and newer Windows versions.

Upvotes: 0

Related Questions