How to Delete Files After X Days Using PS Script

I'd like to change this PowerShell script so that it just deletes files, not directories, after X days. I don't want a folder erased, even if it's empty. Simply the files themselves. How can I change the code below to just work with files, including hidden and read-only files? - Isn't Force already covering hidden and read-only? So all I want to do is undo the folder deletions.

Best Answered by

Dany· Answered on Feb 03, 2023

PowerShell allows you to gain complete control of the operating system with the help of a Shell that starts inside a Command prompt window, including deleting files on Windows with command-let or Cmdlets syntaxes, which are the built-in PowerShell commands that help you do specific deletion actions. As a result, whenever you need to delete a file with PowerShell, these are the Cmdlets to utilize.

To delete a single file or folder from a specific path, use the "-path" argument to "Remove-Item" to provide the exact path of the file. This is the greatest example of how to delete a single file or folder in PowerShell using the Remove-Item cmdlet. For example:

> Remove-Item -Path C:\\New\\Docs.txt

get-childitem $Path -Recurse -File |

where {$_.lastwritetime -lt (get-date).adddays($HowOld)} |

foreach {remove-item $_.fullname -force -whatif}

The PowerShell version - $PSversionTable can be the best to create a PowerShell script. And You can add the '-file' to the get-childitem, but it needs PSv3+, which will only grab files, not folders. If less than Psv3, you can go after mode. But I also thought the - not $_.psiscontainer should have prevented folders from getting deleted. But I have not tested it.

The Remove-Item command will use the wild card "*" to remove the folder's folders and files. You should substitute "*" for the file extension and name. Finally, the execution of the following command will assist in deleting all files and folders from the folder path.

If some of your files are corrupted during the script running, you can learn how to delete corrupted files by various methods. However, if you enter the wrong path and accidentally erase some critical files, stop using your hard drive immediately and use a file recovery application to retrieve them. EaseUS Data Recovery Wizard is a professional data recovery tool for recovering deleted, lost, formatted, inaccessible, and corrupted files.

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Delete Files on a Particular Date with Unix

If you have a list of files but only want to delete files older than a particular date with Unix. This post teaches you the specific command line to run a helpful script. You can also protect your data security with the help of EaseUS Data Recovery Wizard.


Jan 20, 2023

Quickly Delete Massive File Structure on Windows Server 2012

Normally, the deletion of large files takes a long time. Your computer needs to confirm and analyze the data, which will take time. The methods below may help you quickly delete massive files structure on Windows Server 2012.


Jan 10, 2023

What Is the Use of Shift Delete on Windows

We know many shortcuts can be used on Windows, and we are going to introduce the use of Shift + Delete on Windows. We will also let you know when and why you need to use this shortcut on Windows in this passage.


Jan 31, 2023

How to Fix 'Can't Delete a File on Windows Server' Issue

It will sometimes happen that you cannot access or delete a specific file suddenly. We think the methods below will help you get out of this situation, and you can have a try.


Jan 11, 2023

How to Fix Can't Delete a File That's Stuck on Desktop

There are several methods to delete a file that is stuck on your desktop, and we will show you some possible solutions to help you troubleshoot this problem in this passage.


Jan 29, 2023

How to Delete a File/Folder at Logon [GPO Script Settings]

It is not a good thing that agents and supervisors can check some sensitive information. We will help you use the logon/logoff script to solve the issue in this passage.


Jan 19, 2023