# Scripts to delete Files and Folders on Windows

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 ](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/)on Windows devices.

 Note: 
**Supported Versions**:

The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

[ ](https://developer.android.com/develop/connectivity/uwb#uwb-enabled_mobile_devices)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

To delete a file
----------------

### Batch Script

Batch script to delete a file

del "filename\_with\_location.ext" 

   1

  del "filename\_with\_location.ext" 

   

 

  

E.g., To delete the file test.txt in the Desktop of the user Deborah,
`<br></br>del “C:\Users\Deborah\Desktop\test.txt”<br></br>`

### PowerShell Script

PowerShell script to delete a file

remove-item "filename.ext" 

   1

  remove-item "filename.ext" 

   

 

  

E.g., To delete the file test.txt in the Desktop of the user Deborah,
`<br></br>remove-item “C:\Users\Deborah\Desktop\test.txt”<br></br>`

 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: Batch script to delete files older than 20 days
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    @echo off forfiles /p "C:\\Users\\username\\Documents\\My Blogs" /s /m \*.docx /d -20 /c "cmd /c del @path" echo Document files older than 20 days deleted pause exit 
    
       1
    
    2
    
    3
    
    
    
      @echo off forfiles /p "C:\\Users\\username\\Documents\\My Blogs" /s /m \*.docx /d -20 /c "cmd /c del @path" echo Document files older than 20 days deleted 
    
    pause 
    
    exit
- The.ps1 script to delete files older than 20 days is as follows: PowerShell script to delete files older than 20 days
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Write-Host "Starting cleanup of old files" # Set target folder and age threshold $path = "C:\\Scripting\\" $DaysThreshold = "-20" # Calculate cutoff date $CurrentDate = Get-Date $CutoffDate = $CurrentDate.AddDays($DaysThreshold) # Find and delete files older than cutoff $files = Get-ChildItem $path -Recurse -Filter \*.bat | Where-Object { $\_.LastWriteTime -lt $CutoffDate } foreach ($file in $files) { Remove-Item $file.FullName -Force Write-Host "Deleted file: $($file.FullName)" } Write-Host "Cleanup completed."
    
       1
    
    2
    
    3
    
    4
    
    5
    
    6
    
    7
    
    8
    
    9
    
    10
    
    11
    
    12
    
    13
    
    14
    
    15
    
    16
    
    17
    
    18
    
    19
    
    20
    
    21
    
    
    
      Write-Host "Starting cleanup of old files"
    
    
    
    \# Set target folder and age threshold
    
    $path = "C:\\Scripting\\"
    
    $DaysThreshold = "-20"
    
    
    
    \# Calculate cutoff date
    
    $CurrentDate = Get-Date
    
    $CutoffDate = $CurrentDate.AddDays($DaysThreshold)
    
    
    
    \# Find and delete files older than cutoff
    
    $files = Get-ChildItem $path -Recurse -Filter \*.bat | Where-Object {
    
     $\_.LastWriteTime -lt $CutoffDate
    
    }
    
    
    
    foreach ($file in $files) {
    
     Remove-Item $file.FullName -Force
    
     Write-Host "Deleted file: $($file.FullName)"
    
    }
    
    
    
    Write-Host "Cleanup completed."

 

To delete a folder
------------------

### Batch Script

Batch script to delete a folder

rmdir /Q/S "foldername\_with\_path" 

   1

  rmdir /Q/S "foldername\_with\_path" 

   

 

  

E.g., To delete a folder test in the Desktop of the user Deborah,
`<br></br>rmdir /Q/S “C:\Users\Deborah\Desktop\test”<br></br>`

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

 

### Powershell Script

PowerShell script to delete a folder

remove-item "foldername\_with\_path" -recurse

   1

  remove-item "foldername\_with\_path" -recurse

   

 

  

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.