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 22, 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

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.


Feb 22, 2023

How to Delete Files in File Server Automatically

Are you tired of useless files taking up space on your computer? Then, using Linux, and CMD, utilize this straightforward method to delete files older than x days. We can also help you restore inadvertently deleted files on Windows 11/10.


Mar 10, 2023

Is It Possible to Recover Deleted File on Btrfs

You can learn how to recover lost or deleted data from Btrfs hard drives with EaseUS data recovery software. The tool can restore NAS data comprising Synology, QNAP, WD, and more. There is no need to remove RAID disks and rebuild the RAID, and EaseUS offers the most user-friendly methods for restoring NAS data.


Mar 10, 2023

Delete Files Beyond Recovery with 3 Data Erase Tools

When using an HDD, the files you deleted can be recovered easily. If you want to delete files permanently, you can choose the erase tools we recommend. Keep reading to choose a suitable tool.


Mar 09, 2023

How to Recover Deleted Emails Message in Outlook 365 [Microsoft Tips]

Suppose you wish to recover deleted emails that you hard erased (or, more precisely, permanently deleted) from Microsoft Outlook 2016, 2013, or any other Outlook version. Breathe easily. Outlook's Deleted Items folder and the email recovery program EaseUS Data Recovery Wizard both allow you to retrieve deleted emails.


Mar 10, 2023

How to Delete Files and Folders with Exclusion Circumstances

Do you want to learn how to remove a file with an exclusion circumstance using PowerShell? Don't panic because this answer will show you how to delete numerous files with the Remove-item cmdlet and delete files that do not match the criteria.


Mar 10, 2023