@@ -15,6 +15,10 @@ This ensures maximum reliability when removing items across different providers.
1515The file or directory path to remove. Can be a filesystem path or provider path.
1616Accepts pipeline input and wildcards. Must be a valid, non-empty path.
1717
18+ . PARAMETER CountRebootDeletionAsSuccess
19+ If specified, the function returns $true when a file is successfully marked for deletion on reboot.
20+ By default ($false), the function returns $false in this scenario.
21+
1822. EXAMPLE
1923Remove-ItemWithFallback -Path "C:\temp\myfile.txt"
2024Attempts to remove the file using all available methods.
@@ -40,7 +44,12 @@ function Remove-ItemWithFallback {
4044 )]
4145 [ValidateNotNullOrEmpty ()]
4246 [Alias (" FullName" )]
43- [string ]$Path
47+ [string ]$Path ,
48+ # #######################################################################
49+ [Parameter (
50+ Mandatory = $false
51+ )]
52+ [switch ]$CountRebootDeletionAsSuccess = $false
4453 # #######################################################################
4554 )
4655
@@ -50,8 +59,7 @@ function Remove-ItemWithFallback {
5059 $Path = GenXdev.FileSystem\Expand-Path $Path
5160 }
5261
53-
54- process {
62+ process {
5563 try {
5664 # verify item exists and get its provider information
5765 $item = Microsoft.PowerShell.Management\Get-Item - LiteralPath $Path `
@@ -64,16 +72,40 @@ process {
6472
6573 # try fastest method first - direct file deletion
6674 if ([System.IO.File ]::Exists($Path )) {
67- [System.IO.File ]::Delete($Path )
68- Microsoft.PowerShell.Utility\Write-Verbose " Successfully removed file using IO: $Path "
69- return $true
75+ try {
76+ [System.IO.File ]::Delete($Path )
77+ Microsoft.PowerShell.Utility\Write-Verbose " Successfully removed file using IO: $Path "
78+ return $true
79+ }
80+ catch {
81+ # If ErrorAction Stop was specified, immediately rethrow
82+ if (($PSBoundParameters.ContainsKey (' ErrorAction' ) -and $PSBoundParameters [' ErrorAction' ] -eq ' Stop' ) -or
83+ $ErrorActionPreference -eq ' Stop' ) {
84+ throw
85+ }
86+ # Otherwise, fall through to next deletion method
87+ Microsoft.PowerShell.Utility\Write-Verbose " Direct file deletion failed: $_ "
88+ # Don't rethrow here - let the code flow to the next deletion method
89+ }
7090 }
7191
7292 # handle directory deletion with recursive option
7393 if ([System.IO.Directory ]::Exists($Path )) {
74- [System.IO.Directory ]::Delete($Path , $true )
75- Microsoft.PowerShell.Utility\Write-Verbose " Successfully removed directory using IO: $Path "
76- return $true
94+ try {
95+ [System.IO.Directory ]::Delete($Path , $true )
96+ Microsoft.PowerShell.Utility\Write-Verbose " Successfully removed directory using IO: $Path "
97+ return $true
98+ }
99+ catch {
100+ # If ErrorAction Stop was specified, immediately rethrow
101+ if (($PSBoundParameters.ContainsKey (' ErrorAction' ) -and $PSBoundParameters [' ErrorAction' ] -eq ' Stop' ) -or
102+ $ErrorActionPreference -eq ' Stop' ) {
103+ throw
104+ }
105+ # Otherwise, fall through to next deletion method
106+ Microsoft.PowerShell.Utility\Write-Verbose " Direct directory deletion failed: $_ "
107+ # Don't rethrow here - let the code flow to the next deletion method
108+ }
77109 }
78110 }
79111 }
@@ -90,13 +122,20 @@ process {
90122 catch {
91123 Microsoft.PowerShell.Utility\Write-Verbose " Standard deletion failed, attempting boot-time removal..."
92124
125+ # Check if ErrorAction Stop was specified via parameter or preference variable
126+ if (($PSBoundParameters.ContainsKey (' ErrorAction' ) -and $PSBoundParameters [' ErrorAction' ] -eq ' Stop' ) -or
127+ $ErrorActionPreference -eq ' Stop' ) {
128+ # Rethrow the original exception immediately without trying fallback methods
129+ throw
130+ }
131+
93132 # only try boot-time deletion for filesystem items
94133 if ((Microsoft.PowerShell.Management\Get-Item - LiteralPath $Path ).PSProvider.Name -eq ' FileSystem' ) {
95134
96135 # last resort - mark for deletion on next boot
97136 if (GenXdev.FileSystem\Remove-OnReboot - Path $Path ) {
98137 Microsoft.PowerShell.Utility\Write-Verbose " Marked for deletion on next reboot: $Path "
99- return $true
138+ return [ bool ] $CountRebootDeletionAsSuccess
100139 }
101140 }
102141
0 commit comments