# copy only latest backup

**URL:** <https://forums.powershell.org/t/copy-only-latest-backup/15595>\
**Category:** PowerShell Help\
**Created:** [February 5, 2021, 11:19pm UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595 "2021-02-05T23:19:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercyangel](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mercyangel](https://forums.powershell.org/u/mercyangel)\
**Post date:** [February 5, 2021, 11:19pm UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/1 "2021-02-05T23:19:30Z")

</div>

i am having some windows backup folder and want to put it on some samba share folder to create a backups, problem is that in windows it creates every day backups, and i want only the latest. Files are named like: Backkup1.zip, Backkup2.zip, etc.  
where is mistake? This copy all content, not only the newest one, and not only Backkup\*.zip files, i put some txt and that copy also

> $backuppath = "D:Backups" get-childitem -path $backuppath
> 
> get-childitem -path $backuppath -Filter “Backkup\*.zip” |
> 
> where-object { -not $\_.PSIsContainer } |
> 
> sort-object -Property $\_.CreationTime |
> 
> select-object -last 1 | pscp -pw mypass -r $backuppath [user@192.168.12.20](mailto:user@192.168.12.20):/mnt/DatabaseBackup

  
&nbsp;

---

<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:** [February 6, 2021, 12:51am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/2 "2021-02-06T00:51:25Z")

</div>

In your last code line you’re using $backuppath. I think it should be $\_.FullName instead. 😉 … and I think you should wrap this last command in a Foreach-Object scriptblock.

---

<div class="post-metadata">

**Author:** ![mercyangel](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mercyangel](https://forums.powershell.org/u/mercyangel)\
**Post date:** [February 6, 2021, 6:11am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/4 "2021-02-06T06:11:17Z")

</div>

but i want to use whole folder cant understand what you mean

---

<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:** [February 6, 2021, 6:36am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/5 "2021-02-06T06:36:29Z")

</div>

> [@](#):
>
> but i want to use whole folder cant understand what you mean

I understood you only wanted the the latest file?

---

<div class="post-metadata">

**Author:** ![mercyangel](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mercyangel](https://forums.powershell.org/u/mercyangel)\
**Post date:** [February 6, 2021, 6:44am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/7 "2021-02-06T06:44:59Z")

</div>

latest file in that folder, yes.

---

<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:** [February 6, 2021, 6:49am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/8 "2021-02-06T06:49:53Z")

</div>

[quote quote=290968]latest file in that folder, yes.  
[/quote]

OK, and what does “pscp” do?

---

<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:** [February 6, 2021, 6:50am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/9 "2021-02-06T06:50:40Z")

</div>

-

---

<div class="post-metadata">

**Author:** ![mercyangel](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mercyangel](https://forums.powershell.org/u/mercyangel)\
**Post date:** [February 7, 2021, 9:28pm UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/11 "2021-02-07T21:28:19Z")

</div>

pscp for transfering files. Understand what you mean, if i want to catch a file to copy, then to work with files, not to use folder.  
Can you help with that last part

---

<div class="post-metadata">

**Author:** ![mercyangel](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mercyangel](https://forums.powershell.org/u/mercyangel)\
**Post date:** [February 7, 2021, 9:30pm UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/12 "2021-02-07T21:30:33Z")

</div>

can you help with that last part.

pscp is just used for transfering files

---

<div class="post-metadata">

**Author:** ![james\_yumnam](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@james\_yumnam](https://forums.powershell.org/u/james_yumnam)\
**Post date:** [February 8, 2021, 8:18am UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/13 "2021-02-08T08:18:44Z")

</div>

I think the mistake in your script is although you have selected the latest backup file you are passing the folder path only and not the selected file path to pscp.

" pscp -pw mypass -r $backuppath [user@192.168.12.20](mailto:user@192.168.12.20):/mnt/DatabaseBackup"

**$backuppath** still has **D:Backups.** So pscp is copying the all the contents from this folder(including .txt files or others as you have said) and that too recursively because you have **-r** argument as well (I think this is not required.)

this should work: pscp -pw mypass $\_.FullName [user@192.168.12.20](mailto:user@192.168.12.20):/mnt/DatabaseBackup

---

<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:27pm UTC](https://forums.powershell.org/t/copy-only-latest-backup/15595/14 "2024-05-16T20:27:53Z")

</div>


