# Having problems with script to delete files older than X on remote servers

**URL:** <https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644>\
**Category:** PowerShell Help\
**Created:** [November 8, 2018, 12:36pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644 "2018-11-08T12:36:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 8, 2018, 12:36pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644/1 "2018-11-08T12:36:54Z")

</div>

I know this might be simple for most of you, but I’m having a problem creating a script that will check for files older than X on remote servers and delete them.

I’m able to get it to work in terms of gathering directory information based on date:

# This gets the sever list from my text file

**$servers = Get-Content -path “c:\temp\servers.txt”**

# This takes the server list and loops it to look for files in c:\temp. I get a list back of files and folders for all servers in the list.

**foreach ($server in $servers) {Invoke-Command -ComputerName $server {Get-ChildItem -path “c:\temp” -Recurse | Where-Object {$\_.LastWriteTime -lt (get-date).AddDays(-15)}}}**

\*\*\*\* This is where I have a problem \*\*\*\*\*\*\*  
When I add the section for “Remove-Item”, it ignores the date stamp and just deletes everything in the directory (files and folders).

Here is that line:  
foreach ($server in $servers) {Invoke-Command -ComputerName $server {Get-ChildItem -path “c:\temp” -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-15) ; **Remove-Item -Recurse -Force $** _ **.FullName** }}}

Any ideas what I’m doing wrong?

Thanks in advance for anyone’s help! 🙂

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 8, 2018, 12:45pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644/2 "2018-11-08T12:45:31Z")

</div>

Here is something that is ready made,

> **[Browse code samples](https://docs.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery)**
>
> Get started with Microsoft developer tools and technologies. Explore our samples and discover the things you can build.

---

<div class="post-metadata">

**Author:** ![wes-stahler](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wes-stahler/32/227_2.png) [@wes-stahler](https://forums.powershell.org/u/wes-stahler)\
**Post date:** [November 8, 2018, 12:53pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644/3 "2018-11-08T12:53:55Z")

</div>

Couple things:  
Check out the help on Invoke-Command for the parameter ComputerName. It can take an array.  
The other item is that you need to pipe to Remove-Item.  
ScriptBlock should looks like:[pre]  
Get-ChildItem –Path “C:\temp” -Recurse | Where-Object {($\_.LastWriteTime -lt (Get-Date).AddDays(-15))} | Remove-Item  
[/pre]Hope this helps!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 8, 2018, 3:57pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644/4 "2018-11-08T15:57:46Z")

</div>

Thank you KVprasoon and Wes for replying.

I ended up using Wes’ example and it worked perfectly.

---

<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:34pm UTC](https://forums.powershell.org/t/having-problems-with-script-to-delete-files-older-than-x-on-remote-servers/11644/5 "2024-05-16T20:34:09Z")

</div>

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


