Category Filter

Scripts to delete Files and Folders on Windows 10

When the number of files/folders in an organization increases, the administrators might need to delete outdated files. With more and more machines getting remote, admins find this task increasingly difficult. Hexnode lets admins handle this action via scripts on Windows devices.

Disclaimer:

The Sample Scripts provided below are adapted from third-party Open-Source sites.

To delete a file

.bat format

E.g., To delete the file test.txt in the Desktop of the user Deborah,

del “C:\Users\Deborah\Desktop\test.txt”

.ps1 format

E.g., To delete the file test.txt in the Desktop of the user Deborah,

remove-item “C:\Users\Deborah\Desktop\test.txt”

Notes:

  • The admin can also delete files older than a specific number of days.
    The .bat script to delete .docx files that are older than 20 days from the location C:\Users\username\Documents\My Blogs is as follows:
  • The.ps1 script to delete files older than 20 days is as follows:

To delete a folder

.bat format

E.g., To delete a folder test in the Desktop of the user Deborah,

rmdir /Q/S “C:\Users\Deborah\Desktop\test”

Notes:

/S deletes all the files from the folder. /Q makes sure that the user is not asked for confirmation for deleting the folder.

.ps1 format

E.g., To delete a folder test in the Desktop of the user Deborah,

remove-item “C:\Users\Deborah\Desktop\test” -recurse

Notes:

  • The –recurse attribute ensures that all the files inside the folder are deleted.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.