PowerShell Script to Delete All Files and Folders in a directory

This PowerShell script deletes all folders and files in a spesific directory. Before deleting the folders and files script creates a csv file and export the details about deleted items. Priviliged account is needed to run the script. And automate process of the script is also described in the post.

Jan 18, 2023 - 16:40
Feb 20, 2023 - 13:57
PowerShell Script to Delete All Files and Folders in a directory
PowerShell Script - Delete Files and Folders

This PowerShell script deletes all folders and files in a spesific directory. Before deleting the folders and files script creates a csv file and export the details about deleted items. Priviliged account is needed to run the script.

It is important to check carafully the directory of the folder to be deleted.

See the script in github:

powershell/delete_folderandfiles.ps1 at main · kbsuperuser/powershell (github.com)

*******

<#
.SYNOPSIS
Delete Folder and Files
.DESCRIPTION
This PowerShell script deletes all folders and files in a spesific directory. Before deleting the folders and files script creates a csv file and export the details about deleted items. Priviliged account is needed to run the script.
.EXAMPLE
PS> ./delete_folderandfiles
.LINK
https://github.com/kbsuperuser/powershell
.NOTES
Author: kbsuperuser.com | License: CC0
#>
# Step 1: Export folder and file information to CSV
$directory = "C:\example\folder\"
$files = Get-ChildItem -Path $directory -Recurse | Select-Object -Property Name, Extension, CreationTime, LastWriteTime, @{Name='CreatedBy'; Expression={(Get-Acl $_.FullName).Owner}}, @{Name='UpdatedBy'; Expression={(Get-Acl $_.FullName).Access | Where-Object {$_.IsInherited -eq $false} | Select-Object -ExpandProperty IdentityReference}}, @{Name='Owner'; Expression={(Get-Acl $_.FullName).Owner}}
$files | Export-Csv -Path "$directory\file_information.csv" -NoTypeInformation
# Step 2: Delete all files and folders
Remove-Item -Recurse -Force $directory

*******

AUTOMATE THE SCRIPT

  1. Task Scheduler: You can use the built-in Task Scheduler in Windows to schedule your script to run weekly.

    • Open the Task Scheduler by typing "Task Scheduler" in the Start menu search bar.
    • Click on "Create Basic Task" on the right side of the window.
    • Give the task a name and description, then click "Next."
    • Select "Weekly" as the trigger, then click "Next."
    • Select the day of the week and the time you want the script to run, then click "Next."
    • Select "Start a program" as the action, then click "Next."
    • In the "Program/script" field, type the full path to the PowerShell executable (e.g. "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"). In the "Add arguments (optional)" field, type the full path to the script file (e.g. "C:\example\folder\script.ps1").
    • Click on Finish to complete.
  2. PowerShell Scheduled Job: You can also use the built-in scheduled job feature in PowerShell to schedule your script to run weekly.

    • Open PowerShell as an administrator.
    • Type the following command to create a new scheduled job:
      • Register-ScheduledJob -Name "Weekly Script" -ScriptBlock {& 'C:\example\folder\script.ps1'} -Trigger (New-JobTrigger -Weekly -DaysOfWeek Monday -At "02:00")
  3. Cron jobs: If you are running Windows server and have installed the windows subsystem for Linux (WSL), you can use cron jobs as well.

    • Open the WSL terminal
    • Type the following command to open the crontab:
      • crontab -e
      • 0 2 * * 1 powershell.exe -Command "& 'C:\example\folder\script.ps1'"

IMPORTANT NOTICE:

Keep in mind that when automating a script that deletes files, it is always a good practice to make sure that the script is working properly and tested before scheduling it to run.

Also, make sure that you have set the correct permissions for the script and the files that you want to delete.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow