2

Good afternoon,

I need to create a script to delete files older than 30 days from the Downloads folder for all users on a server.

I used the command below but it didn't work. Any idea what could be wrong?

Get-ChildItem "\\ server \ folder1 \ User_Data \ * \ Downloads \ *" -Recurse |
    Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} |
        Remove-Item -ErrorAction SilentlyContinue
  • What's the output when you run the command? Does it give an error or does it run without error but not do what you're expecting? – boog Mar 6 at 17:55
  • 1
    Leandro -- Give this a shot and tell me if it work.... Get-ChildItem "\\server\folder1\User_Data\*\Downloads" -Directory | % {Get-ChildItem $_.FullName -Filter "*" | Where LastWriteTime -lt (Get-Date).AddDays(-30) | Remove-Item -Force -ErrorAction SilentlyContinue;}; If so, I'll be happy to add as an answer that you can accept rather than in this comment area. – Pimp Juice IT Mar 6 at 19:07
  • 1
    So break down the command! Does Get-ChildItem return files? Does your Where-Object limit the results? Trouble-shooting 101! – Keith Miller Mar 6 at 19:57
  • 2
    Maybe you shouldn't silence the error action of Remove-Item`. There's probably an error message with useful information... – Keith Miller Mar 7 at 0:11
  • 1
    Also explain why it didn't work, what exactly is not working? Is it not working at all 100%, just not deleting some files within certain subfolders or what. I also agree about removing the -ErrorAction SilentlyContinue as Keith mentioned in case that's hiding the error detail you need to see for further troubleshooting. It could be "Access Denied", etc. I provided you a similar script I used for something and the way I handled that so simply give it a try and see what your result may be afterwards. Have you already done so? – Pimp Juice IT Mar 7 at 14:27
0

So, what happens if you do this...?

Get-ChildItem 'D:\Test\Reference\New folder' -Recurse
<#
# Results

    Directory: D:\Test\Reference\New folder


Mode                LastWriteTime         Length Name                                                                                                          
----                -------------         ------ ----                                                                                                          
d-----        06-Mar-20     10:52                New folder (2)                                                                                                
-a----        10-Jan-20     17:59              0 awél.txt                                                                                                      
-a----        04-Jan-20     02:01             39 hello.bat                                                                                                     
-a----        04-Jan-20     01:43             44 hello.ps1                                                                                                     
-a----        04-Jan-20     02:16             64 mytest - Copy.txt                                                                                             
-a----        04-Jan-20     02:16             64 mytest.txt                                                                                                    


    Directory: D:\Test\Reference\New folder\New folder (2)


Mode                LastWriteTime         Length Name                                                                                                          
----                -------------         ------ ----                                                                                                          
-a----        10-Jan-20     17:59              0 awél.txt                                                                                                      
-a----        04-Jan-20     02:01             39 hello.bat                                                                                                     
-a----        04-Jan-20     01:43             44 hello.ps1                                                                                                     
-a----        04-Jan-20     02:16             64 mytest - Copy.txt                                                                                             
-a----        04-Jan-20     02:16             64 mytest.txt   
#>

Get-ChildItem 'D:\Test\Reference\New folder' -Recurse | 
Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-1))} | 
Remove-Item -WhatIf

<#
# Results

What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\awél.txt".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\hello.bat".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\hello.ps1".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\mytest - Copy.txt".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\mytest.txt".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\New folder (2)\awél.txt".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\New folder (2)\hello.bat".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\New folder (2)\hello.ps1".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\New folder (2)\mytest - Copy.txt".
What if: Performing the operation "Remove File" on target "D:\Test\Reference\New folder\New folder (2)\mytest.txt".
#>

Get-ChildItem 'D:\Test\Reference\New folder' -Recurse | 
Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-1))} | 
Remove-Item -Confirm:$false -Force
Get-ChildItem 'D:\Test\Reference\New folder' -Recurse
<#
# Results
    Directory: D:\Test\Reference\New folder


Mode                LastWriteTime         Length Name                                                                                                          
----                -------------         ------ ----                                                                                                          
d-----        06-Mar-20     10:51                New folder (2) 
#>
| improve this answer | |
0

This would work better:

$Path = "\\server\folder1\User_Data\*\Downloads"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.