Windows Server 2012

How Can I Delete a Folder or File from the Command Line Using Windows PowerShell?

In this Ask the Admin, I’ll show you how to use Windows PowerShell to delete one or multiple folders from the command line.

Use PowerShell to Delete a Single File or Folder

Let’s start by running a simple command to delete a single file or folder. Make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.

  • Open a PowerShell prompt by switching to the Start screen and typing PowerShell. In the search results, make sure PowerShell is highlighted and press Enter.
  • In the PowerShell console, type Remove-Item –path c:\testfolder –recurse and press Enter, replacing c:\testfolder with the full path to the folder you want to delete.

The –recurse parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.

  • To check the folder has been deleted, type dir c: in the command prompt and press Enter, replacing c: with the parent directory of the folder you just deleted.

More Complex Delete Operations

In the PowerShell prompt, type Remove-Item –path c:\testfolder\ remove-item * -include *.mp3 –recurse and press Enter. This command removes all MP3 files from the testfolder directory and any subdirectories.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

It’s also possible to include a filter, like this:

Remove-Item –path c:\* -Filter *test* -whatif

Deleting a folder using the Remove-Item cmdlet
Deleting a folder using the Remove-Item cmdlet.

Any folder in the root of the C drive with the word test in its name will be deleted. The –whatif parameter tests the command and lets you see the results, so you can be sure you won’t delete anything important. To actually run the delete operation, just remove the –whatif parameter.

Remove-Item –path c:\testfolder\* -include *.txt

The above command will remove all .txt files in the testfolder directory. There is also an –exclude parameter, which comes in handy when used in combination with filters. Finally, to specify the current directory as shown in the PowerShell prompt, just replace the –path parameter with a wildcard symbol as follows:

Remove-Item * -Filter *test*

If you would like to move files using PowerShell, check out How Can I Move a File or Folder from the Command Line Using Windows PowerShell? on Petri.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
Live Webinar - Thursday, December 2nd! Active Directory Masterclass: AD Configuration Strategies for Stronger SecurityREGISTER NOW - Thursday, December 2, 2021 @ 1 pm ET

Active Directory (AD) is leveraged by over 90% of enterprises worldwide as the authentication and authorization hub of their IT infrastructure—but its inherent complexity leaves it prone to misconfigurations that can allow attackers to slip into your network and wreak havoc. 

Join this session with Microsoft MVP and MCT Sander Berkouwer, who will explore:

  • Whether you should upgrade your domain controllers to Windows Server
    2019 and beyond
  • Achieving mission impossible: updating DCs within 48 hours
  • How to disable legacy protocols and outdated compatibility options in
    Active Directory

Sponsored by: