mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-09 13:04:59 +02:00
87 lines
2.3 KiB
Markdown
87 lines
2.3 KiB
Markdown
Script: *remove-dir.ps1*
|
|
========================
|
|
|
|
This PowerShell script removes the given directory tree recursively.
|
|
NOTE: Use with care! This cannot be undone!
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
PS> ./remove-dir.ps1 [[-pathToDir] <String>] [<CommonParameters>]
|
|
|
|
-pathToDir <String>
|
|
Specifies the file path to the directory tree
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Aliases
|
|
Accept wildcard characters? false
|
|
|
|
[<CommonParameters>]
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
```
|
|
|
|
Example
|
|
-------
|
|
```powershell
|
|
PS> ./remove-dir.ps1 C:\Temp
|
|
⏳ Removing directory 'C:\Temp', please wait..."
|
|
✅ Directory C:\Temp\ removed (took 9s).
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Removes a directory
|
|
.DESCRIPTION
|
|
This PowerShell script removes the given directory tree recursively.
|
|
NOTE: Use with care! This cannot be undone!
|
|
.PARAMETER pathToDir
|
|
Specifies the file path to the directory tree
|
|
.EXAMPLE
|
|
PS> ./remove-dir.ps1 C:\Temp
|
|
⏳ Removing directory 'C:\Temp', please wait..."
|
|
✅ Directory C:\Temp\ removed (took 9s).
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
param([string]$pathToDir = "")
|
|
|
|
try {
|
|
if ($pathToDir -eq "" ) { $pathToDir = Read-Host "Enter the file path to the obsolete directory" }
|
|
|
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
|
if (!(Test-Path "$pathToDir" -pathType container)) { throw "Cannot access directory '$pathToDir'" }
|
|
|
|
"⏳ Removing directory '$pathToDir', please wait..."
|
|
Remove-Item -force -recurse -confirm:$false $pathToDir
|
|
if ($lastExitCode -ne 0) { throw "'Remove-Item' failed with exit code $lastExitCode" }
|
|
|
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
|
"✅ Directory $pathToDir removed (took $($elapsed)s)."
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ ERROR: $($Error[0]) in script line $($_.InvocationInfo.ScriptLineNumber)."
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(page generated by convert-ps2md.ps1 as of 08/07/2025 16:01:07)*
|