Patrick Dickey
Patrick Dickey

Reputation: 83

How do I delete a file that PowerShell says does not exist?

I added my Google Drive to my OneDrive and have a file in it that contains an invalid name (con.mp3). When I tried to remove the file (and the directory it is in), I get "Invalid File Handle". So I tried removing it with PowerShell as an Administrator.

Here is the directory listing showing the file, and the results of Remove-Item and del.

PS> dir

    Directory: C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/13/2018  11:49 AM         117069 con.mp3


PS> Remove-Item * -Force
Remove-Item : An object at the specified path C:\Users\Patrick\OneDrive\Google
Drive\CW\CW.15WPM.VeryShortWords\con.mp3 does not exist.
At line:1 char:1
+ Remove-Item * -Force
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveItemCommand

PS> del *.*
del : An object at the specified path C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3 does
not exist.
At line:1 char:1
+ del *.*
+ ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveItemCommand

How do I go about removing this file, so I can remove the directory? I tried removing it from Google Drive, but it didn't sync down to my computer.

Upvotes: 8

Views: 12779

Answers (2)

Pavel Sapehin
Pavel Sapehin

Reputation: 1078

Another thing you could try is to run the following CMD command dir/A/X/P and then run del command against listed MS-DOS short name. So the steps might be:

  1. Open cmd.exe (command prompt)
  2. Change a path to directory that contains the file cd C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords
  3. Run dir/A/X/P to show MS-DOS short name
  4. Delete the file using del command by it's short ms-dos name.

enter image description here

Upvotes: 1

user6811411
user6811411

Reputation:

The reserved word con shouldn't be used as a path / file name (or part of).

You'll have to use the -LiteralPath parameter and eventually prefix with \\?\ when deleting.

So try:

Remove-Item -LiteralPath "\\?\C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3" -Force

If this doesn't work you can try in a cmd window:

Del "\\?\C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3"

If this doesn't help also read:
https://support.microsoft.com/en-us/help/320081/you-cannot-delete-a-file-or-a-folder-on-an-ntfs-file-system-volume

Upvotes: 18

Related Questions