# Move-Item and keep newer file

**URL:** <https://forums.powershell.org/t/move-item-and-keep-newer-file/19413>\
**Category:** PowerShell Help\
**Created:** [May 24, 2022, 8:59am UTC](https://forums.powershell.org/t/move-item-and-keep-newer-file/19413 "2022-05-24T08:59:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom129](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@Tom129](https://forums.powershell.org/u/Tom129)\
**Post date:** [May 24, 2022, 8:59am UTC](https://forums.powershell.org/t/move-item-and-keep-newer-file/19413/1 "2022-05-24T08:59:37Z")

</div>

Hi everybody 🙂

Firstly, thank you so much for you help recently. But I have a new problem, still on the same script. I would like to to move file for a directory to a new directory, but when I use Move-Item -Force, if file already exist Powershell keep older File. I’ve found a script with a function that keep newerf file but that doesn’t work. Here my complete script :

```auto
$path = 'C:\Users\margu\Desktop\Test_Copy\*'

$destinationFolder = 'C:\Users\margu\Desktop\Test_Paste\'

$fileList = Get-ChildItem -Path $path -Include tes* -File -Recurse

$hour10 = (Get-Date 10:00).ToString("HH:mm")

# If file already exist, keep newer

function ShouldMove

{

    param($File, $Destination)

    $testPath = Join-Path $Destination -ChildPath $File.Name

    if(Test-Path $testPath)

    {

        # If the destination folder contains this file name

        $destFile = Get-Item $testPath

        if($File.CreationTime -gt $destFile.CreationTime)

        {

            # If the source file is newer return `$true`

            return $true

        }

       

        # Else `$false`

        return $false

    }

    # If destination does not have this file, return `$true`

    $true

}

# Test if folder exist

if (!(Test-Path $destinationFolder))

{

  New-Item -Path $destinationFolder -ItemType Directory

}

# Move Files

foreach ($file in $fileList)

{

    $dateFile = (Get-Item -Path $file).LastWriteTime

   

    $hourFile = $dateFile.ToString("HH:mm")

   

    # Set name according next day or current day

    if ($hourFile -ge $hour10)

    {

      $name = ($file.LastWriteTime.AddDays(1).ToString('ddMMyyyy')) + $file.Extension

    }

   

    else

    {

      $name = ($file.LastWriteTime.ToString('ddMMyyyy')) + $file.Extension

    }

    $destinationFile = Join-Path -Path $destinationFolder -ChildPath $name

   

    if(-not $destinationFile) { continue }

    if(ShouldMove -File $file -Destination $destinationFile)

    {

        # `-Force` to replace it

        Move-Item -LiteralPath $file.FullName -Destination $destinationFile -Force

        continue

    }

    # If `ShouldMove` returned `$false` then, instead of moving the file, delete the source

    Remove-Item -LiteralPath $destinationFile.FullName

}

```

So how can I keep the newer file in new directory with this script.

Thank you for your help and have a good day 🙂

---

<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:** [May 24, 2022, 9:39am UTC](https://forums.powershell.org/t/move-item-and-keep-newer-file/19413/2 "2022-05-24T09:39:14Z")

</div>

Without digging too deep into your code … robocopy is made to copy files or folders very efficiently and has a lot of options to control what should be copied. 😉 A script may not be necessary.

I noticed you’re using the property `CreationTime` of the files to be compared against each other. In most of the cases this is the wrong property. Are you really sure you don’t need to use `LastWriteTime` instead?

---

<div class="post-metadata">

**Author:** ![Tom129](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@Tom129](https://forums.powershell.org/u/Tom129)\
**Post date:** [May 24, 2022, 2:59pm UTC](https://forums.powershell.org/t/move-item-and-keep-newer-file/19413/3 "2022-05-24T14:59:41Z")

</div>

Thany you to get noticed for the CreationTime instead of LastWriteTime but that didn’t solve the problem. I’ve search solution on Google but nothing solve this for keep the newest. I understand Olaf that I can simplify the script, but I want to learn to use Powershell.

---

<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:** [May 24, 2022, 3:32pm UTC](https://forums.powershell.org/t/move-item-and-keep-newer-file/19413/4 "2022-05-24T15:32:47Z")

</div>

> [@Tom129](#):
>
> I’ve found a script with a function that keep newerf file but that doesn’t work.

Just because someone published or posted code online does not mean the code is correct and working. 😉

> [@Tom129](#):
>
> but I want to learn to use Powershell.

It does not make sense to use code from the internet you don’t fully understand and you cannot be sure about if it works at all for the purpose you’re looking for. AND it does not make sense to rebuilt already existing built in functionality. There’s no benefit in it. If it’s about a copy job - use robocopy. 😉

> [@Tom129](#):
>
> when I use Move-Item -Force, if file already exist Powershell keep older File.

**No.** If you use the parameter `-Force` PowerShell will copy the file no matter if it’s newer or not.

OK. Let’s do a big step back and start from scratch - what do you actually want to achieve?

---

<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/move-item-and-keep-newer-file/19413/5 "2024-05-16T20:17:55Z")

</div>


