Functions/GenXdev.FileSystem/Remove-AllItems.ps1
|
<############################################################################## Part of PowerShell module : GenXdev.FileSystem Original cmdlet filename : Remove-AllItems.ps1 Original author : René Vaessen / GenXdev Version : 3.29.2026 ################################################################################ Copyright (c) 2026 René Vaessen / GenXdev Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ################################################################################> ############################################################################### <# .SYNOPSIS Recursively removes all content from a directory with advanced error handling. .DESCRIPTION Safely removes all files and subdirectories within a specified directory .PARAMETER Path The directory path to clear. Can be relative or absolute path. Will be normalized and expanded before processing. .PARAMETER DeleteFolder When specified, also removes the root directory specified by Path after clearing its contents. .PARAMETER WhatIf Shows what would happen if the cmdlet runs. The cmdlet is not run. .EXAMPLE Remove-AllItems -Path "C:\Temp\BuildOutput" -DeleteFolder -Verbose .EXAMPLE sdel ".\temp" -DeleteFolder #> function Remove-AllItems { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] [Alias('sdel')] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] param( ############################################################################### [Parameter( Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The directory path to clear' )] [ValidateNotNullOrEmpty()] [Alias('FullName')] [string] $Path, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Also delete the root folder supplied with the Path parameter' )] [switch] $DeleteFolder ############################################################################### ) begin { # convert relative or shorthand paths to full filesystem paths $Path = GenXdev\Expand-Path "$Path\" Microsoft.PowerShell.Utility\Write-Verbose "Normalized path: $Path" } process { try { if (![System.IO.Directory]::Exists($Path)) { Microsoft.PowerShell.Utility\Write-Verbose "Directory does not exist: $Path" return } Microsoft.PowerShell.Utility\Write-Verbose "Processing directory: $Path" if (-not $PSCmdlet.ShouldProcess($Path, 'Delete directory')) { return } $null = cmd.exe /c rd /S /Q "$Path" 2>&1 if (-not $DeleteFolder) { $null = GenXdev\Expand-Path "$Path\" -CreateDirectory } } catch { GenXdev\Show-Exception -Exception $_.Exception } } end { } } |