如果您无法从计算机中删除文件或文件夹,您可以使用Windows PowerShell轻松删除任何文件和文件夹。使用PowerShell的优点是您可以强制删除文件(force delete a file)并从文件夹中删除所有项目。我们已经了解了如何使用命令提示符删除文件和文件夹。现在让我们看看如何使用PowerShell来完成。
使用 PowerShell(Use PowerShell)删除文件和文件夹
要使用Windows PowerShell删除文件和文件夹(delete files and folders),请执行以下步骤 -
- 打开 Windows PowerShell
- 识别文件夹
- 使用删除项目命令。
这是详细的教程形式。
(Delete)使用PowerShell(PowerShell)删除单个文件
要开始使用,您需要打开PowerShell。为此,您可以按Win+X ,然后从列表中选择Windows PowerShell 。或者,您可以在任务栏(Taskbar)搜索框中搜索它。之后,输入以下命令-
Remove-item file-path
这是命令的基本形式。
假设您在桌面(Desktop)的TWC文件夹中有一个名为TWC.png的文件。要使用Windows PowerShell(Windows PowerShell)删除它,您需要输入以下命令 -
Remove-item C:\Users\user-name\Desktop\TWC\TWC.png
您必须在命令中包含文件扩展名。否则,它将无法识别您的命令,并且会显示一条欢迎消息来欢迎您。
相关(Related):如何在 Windows中删除超过 X 天的文件
(Delete)使用PowerShell(PowerShell)删除单个文件夹
如果要使用Windows PowerShell删除文件夹,则需要输入前面提到的命令-
Remove-Item folder-path
假设您的桌面上有一个名为(Desktop)TWC的文件夹。如果要删除它,则需要输入此命令-
Remove-item C:\Users\user-name\Desktop\TWC
如果文件夹为空,将立即删除。但是,如果文件夹中有一些文件,您需要按Y和Enter按钮确认删除。
(Delete)使用PowerShell(PowerShell)删除多个文件
如果要删除多个文件,则需要输入不同的命令。该命令看起来相同,但有一点不同。
Remove-item file-path, file-path1, file-path2
您需要像这样输入所有文件路径。例如,您在桌面上有一个名为(Desktop)TWC.png的文件,在下载(Downloads)文件夹中有另一个名为TWC1.txt的文件。要立即删除它们,您可以输入以下命令-
Remove-item C:\Users\user-name\Desktop\TWC.png, C:\Users\user-name\Downloads\TWC1.txt
(Delete)使用PowerShell(PowerShell)删除多个文件夹
与删除多个文件一样,使用Windows PowerShell(Windows PowerShell)删除多个文件夹的命令相同。例如,您可以使用这样的命令-
Remove-item C:\Users\user-name\Desktop\TWC, C:\Users\user-name\Downloads\TWC1
此命令将立即从Desktop中删除(Desktop)TWC文件夹,并从Downloads文件夹中删除(Downloads)TWC1文件夹。
供您参考,如果您使用这些命令,您的文件和文件夹将被永久删除。换句话说,您无法在回收站(Recycle Bin)中找到它们。
检查文件夹内的项目
如果您无法打开文件夹,但您想了解项目,您可以使用此命令 -
Get-ChildItem folder-path
如果您的桌面(Desktop)上有一个名为TWC的文件夹,则需要输入以下命令以显示所有内容-
Get-ChildItem C:\Users\user-name\Desktop\TWC
默认情况下,它显示Mode、LastWriteTime、Length和Name。您可以从此处获取文件名,以便删除正确的项目。
检查(Check)上次修改时间和创建时间
如果要检查文件的创建或修改时间,可以使用此命令 -
Get-ChildItem C:\Users\user-name\Desktop\TWC | Select-Object FullName, LastWriteTime, CreationTime
您可以找到包含所有文件名和其他详细信息的三列。
强制删除项目
如果您的文件夹中有一些您想要删除的只读或隐藏文件,则不能使用上述命令,因为它会显示错误。在这种情况下,您必须使用-force参数。例如,如果桌面上的(Desktop)TWC文件夹中有一些隐藏或只读文件,则必须输入此命令-
Remove-item C:\Users\user-name\Desktop\TWC -force
相同的命令也可用于删除只读文件。无论哪种情况,您都会收到一条确认消息,您需要在其中键入Y并按 Enter 按钮。
删除不确认
如果您使用本文的第一个命令,您将收到一条确认消息。但是,如果您不想收到这样的消息,可以使用一个名为-recurse的参数。
例如,
Remove-item C:\Users\user-name\Desktop\TWC -recurse
输入此命令后,您的文件夹或文件将被永久删除,无需任何确认。
排除某些文件类型
假设您有多种文件类型,例如TXT、PNG、DOCX、PDF等,并且您想要删除除TXT文件之外的所有项目。在这种情况下,您可以使用-exclude参数。命令看起来像这样 -
Remove-Item –path C:\Users\user-name\Desktop\TWC* -exclude *.txt
您可能已经猜到了——它将排除所有.txt文件并删除其他文件。但是,它会显示一条确认消息。如果要直接删除,可以使用前面提到的-recurse参数。(-recurse)
我希望这些命令对您有所帮助。(I hope these commands will be helpful for you.)
Use PowerShell to delete files and folders in Windows 11/10
If you are unable tо delete a file or folder from your computer, you can use Windows PowerShell to delete any file and folder effortlessly. The advantage of using PowerShell is that you can force delete a file and remove all items from inside a folder. We have seen how to delete files & folders using Command Prompt. Now let’s see how to do it using PowerShell.
Use PowerShell to delete files and folders
To delete files and folders using Windows PowerShell, follow these steps-
- Open Windows PowerShell
- Identify the folder
- Use Remove-item command.
Here is the tutorial in detail form.
Delete a single file using PowerShell
To get started, you need to open PowerShell. For that, you can press Win+X, and select Windows PowerShell from the list. Alternatively, you can search for it in the Taskbar search box. After that, enter the following command-
Remove-item file-path
This is the basic form of the command.
Let’s assume that you have a file named TWC.png in the TWC folder on your Desktop. To delete it using Windows PowerShell, you need to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC\TWC.png
You have to include the file extension in the command. Otherwise, it won’t recognize your command, and a welcome message will welcome you.
Related: How to delete Files older than X days in Windows
Delete a single folder using PowerShell
If you want to delete a folder using Windows PowerShell, you need to enter the command as mentioned earlier-
Remove-Item folder-path
Let’s assume that you have a folder named TWC on your Desktop. If you want to delete it, you need to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC
If the folder is empty, it will be deleted immediately. However, if the folder has some files, you need to confirm the delete by pressing Y and Enter buttons.
Delete multiple files using PowerShell
If you have multiple files to delete, you need to enter a different command. The command looks the same, but there is a small difference.
Remove-item file-path, file-path1, file-path2
You need to enter all the file paths like this. For example, you have one file named TWC.png on Desktop and another file named TWC1.txt in the Downloads folder. To delete them at once, you can enter this following command-
Remove-item C:\Users\user-name\Desktop\TWC.png, C:\Users\user-name\Downloads\TWC1.txt
Delete multiple folders using PowerShell
Like deleting multiple files, the command is the same to remove more than one folder using Windows PowerShell. For example, you can use a command like this-
Remove-item C:\Users\user-name\Desktop\TWC, C:\Users\user-name\Downloads\TWC1
This command will delete the TWC folder from the Desktop and TWC1 folder from the Downloads folder at once.
For your information, if you use these commands, your files and folders will be deleted permanently. In other words, you cannot find them in the Recycle Bin.
Check items inside a folder
In case you cannot open a folder, but you want to know about the items, you can use this command-
Get-ChildItem folder-path
If you have a folder on your Desktop named TWC, you need to enter the following command to reveal all things-
Get-ChildItem C:\Users\user-name\Desktop\TWC
By default, it shows the Mode, LastWriteTime, Length, and Name. You can get the file name from here so that you can delete the correct item.
Check last modification time and creation time
If you want to check the time when a file was created or modified, you can use this command-
Get-ChildItem C:\Users\user-name\Desktop\TWC | Select-Object FullName, LastWriteTime, CreationTime
You can find three columns with all the file names and other details.
Force delete an item
If your folder has some read-only or hidden files that you want to remove, you cannot use the command mentioned above, as it will show an error. In that case, you have to use the -force parameter. For example, if there are some hidden or read-only files in the TWC folder that is placed on the Desktop, you have to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC -force
The same command can be used to delete a read-only file, as well. In either case, you will get a confirmation message where you need to type Y and press the Enter button.
Delete without confirmation
If you use the very first command of this article, you will be greeted with a confirmation message. However, if you do not want to get such a message, you can use a parameter called -recurse.
For example,
Remove-item C:\Users\user-name\Desktop\TWC -recurse
After entering this command, your folder or file will be deleted permanently without any confirmation.
Exclude certain file type
Let’s assume that you have multiple file types such as TXT, PNG, DOCX, PDF, etc. and you want to delete all items except the TXT files. In that case, you can use the -exclude parameter. The command looks like this-
Remove-Item –path C:\Users\user-name\Desktop\TWC* -exclude *.txt
You might have already guessed – it will exclude all the .txt files and delete others. However, it shows a confirmation message. If you want to delete them directly, you can use the -recurse parameter as mentioned earlier.
I hope these commands will be helpful for you.