none
Create script to delete files in subfolders RRS feed

  • Question

  • 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

    Friday, March 6, 2020 5:41 PM

Answers

  • Hi guys, I had to add the parameters below for the script to run, I believe that due to the downloads folder having subfolders I was asking for confirmation, adding the parameter -Confirm: $ false the problem was solved. Thank you all.

    Remove-Item -Recurse -Confirm:$false -Force
    • Marked as answer by Leandro.d.c Monday, March 16, 2020 2:17 PM
    Monday, March 16, 2020 2:17 PM

All replies

  • What error do you get? 

    You have "\server". If's that's a UNC path it should be "\\servername\sharename\folder".

    Troubleshoot the script yourself. Execute the commands one by one and see if they produce the output that you expect.

    Does the first command return files? If you add in the where-object, does it show the old files you want to be deleted?

    Get-ChildItem "\\server\folder1\User_Data*\Downloads*" -Recurse 
    Get-ChildItem "\\server\folder1\User_Data*\Downloads*" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} 
    Get-ChildItem "\\server\folder1\User_Data*\Downloads*" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -whatif

    For the correct syntax, see:

    get-help get-childitem 
    get-help get-childitem -examples

     
    • Edited by MotoX80 Friday, March 6, 2020 6:06 PM
    Friday, March 6, 2020 6:03 PM
  • Remove "ErrorAction" to see the errors.

    The path will resolve to the root of the current drive.  As noted use the correct syntax if it is a UNC or add the correct dive letter.  If it is a subfolder of the current folder then drop the first slash or add a dot at the beginning.


    \_(ツ)_/

    Friday, March 6, 2020 6:20 PM
  • I'm sorry, maybe I didn't explain it correctly and the path I specified above is not correct. The correct one would be:

    \\ server \ folder \ User_Data \ all users I need to access \ downloads

    So I used:

    \\ server \ folder \ User_Data \ * \ Downloads \ *


    Users are named as the image below, however there are hundreds and within these folders I need to access and delete only the files in the downloads folder

    

    Friday, March 6, 2020 6:50 PM
  • You can't have spaces in file paths.

    You need to remove the "ErrorAction" from the command then carefully read the errors.


    \_(ツ)_/

    Friday, March 6, 2020 6:52 PM

  • I know, just an error when typing

    Friday, March 6, 2020 7:04 PM
  •  I need to access and delete only the files in the downloads folder

    

    Does the get-childitem command return files? After you get that working add in where-object and verify that you are passing the correct old files to remove-item. 

    Friday, March 6, 2020 7:09 PM
  • I removed the "Remove-Item -ErrorAction SilentlyContinue" parameter and the file information appeared. Strange, because deleting the files is not working
    Friday, March 6, 2020 8:03 PM
  •  Strange, because deleting the files is not working

    -erroraction silentlycontinue suppresses the error messages. Add the remove-item, but leave out the -erroraction.

    Does the account that is running the script have full control permissions on the files? 


    • Edited by MotoX80 Friday, March 6, 2020 8:15 PM
    Friday, March 6, 2020 8:12 PM
  •  Strange, because deleting the files is not working

    -erroraction silentlycontinue suppresses the error messages. Add the remove-item, but leave out the -erroraction.

    Does the account that is running the script have full control permissions on the files? 

    Regarding the removal of the files, I will be able to test only on Monday. When I run the command I will update the information here on the forum. Thanks

    Friday, March 6, 2020 8:24 PM
  • Hi guys, I had to add the parameters below for the script to run, I believe that due to the downloads folder having subfolders I was asking for confirmation, adding the parameter -Confirm: $ false the problem was solved. Thank you all.

    Remove-Item -Recurse -Confirm:$false -Force
    • Marked as answer by Leandro.d.c Monday, March 16, 2020 2:17 PM
    Monday, March 16, 2020 2:17 PM