I have a PowerShell script that writes some results into a file.
Remove-Item
.You can fulfill all the requirements above with rm -f
in Unix-like systems.
First I have tried Remove-Item -Force
, but it couldn't ignore nonexistent files (cf. rm -f
ignores nonexistent files).
PS C:\tmp> Remove-Item C:\tmp\foo.txt -Force
Remove-Item : Cannot find path 'C:\tmp\foo.txt' because it does not exist.
At line:1 char:1
+ Remove-Item C:\tmp\foo.txt -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\tmp\foo.txt:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Next, I have tried Remove-Item -ErrorAction Ignore
and Remove-Item -ErrorAction SilentlyContinue
, but they don't show error messages when they failed to remove the file (cf. rm -f
shows an error message like rm: cannot remove 'foo.txt': Operation not permitted
in this situation).
PS C:\tmp> $file = [System.IO.File]::Open('C:\tmp\foo.txt',[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::None)
PS C:\tmp> Remove-Item C:\tmp\foo.txt -ErrorAction Ignore
# I expected it shows an error because it couldn't remove the file because of the lock, but it showed nothing
PS C:\tmp> $file = [System.IO.File]::Open('C:\tmp\foo.txt',[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::None)
PS C:\tmp> Remove-Item C:\tmp\foo.txt -ErrorAction SilentlyContinue
# I expected it shows an error because it couldn't remove the file because of the lock, but it showed nothing
Is there rm -f
equivalent in PowerShell that fulfills all the requirements above?
To me, the simplest solution is:
if (test-path $file) {
remove-item $file
}
This also occurs to me. $error[0] is always the most recent error.
remove-item $file -erroraction silentlycontinue
if ($error[0] -notmatch 'does not exist') {
write-error $error[0] # to standard error
}
I think you can also use try/catch with specific exceptions. Here's an example. I found the exception through tab completion. But the script will stop with other uncaught exceptions. This error doesn't normally stop.
try { remove-item foo -erroraction stop }
catch [System.Management.Automation.ItemNotFoundException] { $null }
'hi'