Using PowerShell to delete contents from a folder.
Sometimes you need to delete contents from a folder or multiple folders. In this article, we will delete the contents of 4 folders.
You will first need to allow PowerShell to run scripts.
[PowerShell - Set-ExecutionPolicy]
CFFCS | CarrzSynEdit: | PS (PowerShell)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

Next, copy and save this as Remove.ps1
[PowerShell - Remove.ps1]
CFFCS | CarrzSynEdit: | PS (PowerShell)
# Define an array of paths to the folders you want to clear of files.

# Ensure you use full paths (e.g., 'C:\Temp\Folder1', 'D:\Data\Folder2')

$foldersToDeleteFrom = @(
    "G:\VS Projects\Errors\",
    "G:\VS Projects\Covers\",
    "G:\VS Projects\Main\",
	"G:\VS Projects\Temp\"
    # Add more paths as needed

)

# Use -Recurse if you also want to delete files in subfolders.

# Use -Force to delete hidden or read-only files.

# The "/*.*" pattern targets files with an extension, preventing folders from being deleted.


foreach ($folderPath in $foldersToDeleteFrom) {
    if (Test-Path -LiteralPath $folderPath) {
        try {
            # Use Join-Path to add "*" so it targets the INSIDE of the folder

            $contents = Join-Path -Path $folderPath -ChildPath "*"
            
            # Use -Recurse to grab subfolders/files, but the wildcard keeps the root folder

            Remove-Item -Path $contents -Recurse -Force -ErrorAction Stop
            Write-Host "Cleared contents of: $folderPath" -ForegroundColor Green
        }
        catch {
            Write-Host "Failed to clear ${folderPath}: $_" -ForegroundColor Red
        }
    }
}


Write-Host "Script finished." -ForegroundColor Magenta
#====If you want to stop the process at the end, uncomment this next line.

#====This is used in case there is an error.

#Read-Host "Press Enter to exit"

Several options for running this.
  1. Right-click and choose [run with PowerShell]
  2. Run with [PowerShell ISE][li]Right-click the file and choose [Properties]
  3. Click the [Change..] button
  4. Scroll down and choose [Look for another app on this PC]
    Paste this in
    %windir%\system32\WindowsPowerShell\v1.0
    Copy
    Search Site
    Search Google
  5. Click [Enter].
  6. Select [powershell_ise.exe ]and click [Open] (This is the PS Editor. Great for design and editing your Scripts)
  7. Click [Apply] and [OK]
When you double-click the file, it will open the [ISE].