Renamed to 'remove-dir.ps1'

This commit is contained in:
Markus Fleschutz
2025-07-23 09:01:20 +02:00
parent 770bdcb808
commit 2790065a7c
2 changed files with 35 additions and 32 deletions

View File

@ -1,32 +0,0 @@
<#
.SYNOPSIS
Removes a dir tree
.DESCRIPTION
This PowerShell script silently removes a directory tree recursively. Use it with care!
.PARAMETER pathToDirTree
Specifies the file path to the directory tree
.EXAMPLE
PS> ./remove-dir-tree.ps1 C:\Temp
✅ Removed directory 'C:\Temp\ in 9s.
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$pathToDirTree = "")
try {
if ($pathToDirTree -eq "" ) { $pathToDirTree = Read-Host "Enter the path to the directory tree" }
$stopWatch = [system.diagnostics.stopwatch]::startNew()
Remove-Item -Force -Recurse -Confirm:$false $pathToDirTree
if ($lastExitCode -ne 0) { throw "'Remove-Item' failed with exit code $lastExitCode" }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Removed directory '$pathToDirTree' in $($elapsed)s."
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

35
scripts/remove-dir.ps1 Executable file
View File

@ -0,0 +1,35 @@
<#
.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 '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()
"⏳ Removing '$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 in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}