Background
I have a PowerShell script that writes some results into a file.
- I want to remove the result file automatically at the start of the script with
Remove-Item
. - You can remove the result file manually, so I don't want to show error messages even if the result file doesn't exist.
- I want to show error messages when the script couldn't remove the result file for another reason, e.g. the file is locked.
You can fulfill all the requirements above with rm -f
in Unix-like systems.
Problem
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
Question
Is there rm -f
equivalent in PowerShell that fulfills all the requirements above?