Delete All Files in Directory in PowerShell

This tutorial will discuss how to delete all files in a directory using PowerShell.

Using Remove-Item cmdlet

To delete all files in a directory:

  • Use the Remove-Item cmdlet.
  • Include asterisk symbol in the file path.

The above command removes all content in the directory C:\test. If there are sub-directories in the location, it will ask you what to do with them.

Answer Y if you want to remove them and N if you want to keep them.

Delete all files in Directory in PowerShell

If you don’t want prompt, you can use -Recurse parameter with Remove-item cmdlet.

Use -Force parameter, in case you want to remove all items that can not be changed such as hidden or read-only files.

You can use Remove-Item to delete different items in PowerShell, such as files, directories, functions, variables, aliases, and registry keys.

Using the Get-ChildItem cmdlet with Remove-Item

Another way of removing all files in a directory is to:

  • Use Get-ChildItem to get all files or directories in a directory.
  • Pipe the command to Remove-Item.

The above command deletes all files found in the directory C:\test.

The -File parameter helps you to get only files. If not used, the command will get directories too.

To recursively delete files in the directory, use the -Recurse parameter with Get-ChildItem.

To recursively delete all files and sub-directories in the directory, use the -Recurse parameter with Get-ChildItem.

It will remove all files in the C:\test directory, including those in its sub-directories.

Using the Delete() method

The object in PowerShell has the Delete() method, which you can use to remove that object.

To delete all files in a directory:

  • Use Get-ChildItem to get the FileInfo object.
  • Apply the Delete method to it.

Delete All Files with Extension PowerShell

To remove all files having extensions, use dot notation in the file path.

It will not delete any files with no extensions.

You can also select files that have the same type of extensions. For instance, to remove only .txt files, you can use this command.

Alternatively, you can use -include parameter to provide extension of files which you want to delete.
Here is the code for the same:

That’s all about how to remove all files in a directory in PowerShell. We also showed you how to delete specific files using the extensions.

We hope this article is helpful for you. If you have any confusion, let us know in the comments.

Was this post helpful?

Related Posts

  • Show hidden files in PowerShell
    13 January

    Show Hidden Files (dotfiles) with Windows PowerShell

    Table of ContentsUsing Get-ChildItem with the -Force ParameterUsing Get-ChildItem with the Where-Object cmdletUsing ls with the -Force ParameterUsing dir with -ah Switch Using Get-ChildItem with the -Force Parameter To show hidden files with other files, use the Get-ChildItem cmdlet with the -Force or -fo parameter. [crayon-63c4290841066314190691/] [crayon-63c429084106b307290243/] The Get-ChildItem is a PowerShell cmdlet that lists […]

  • Merge multiple CSV files in PowerShell
    13 January

    Merge Multiple CSV Files in PowerShell

    Table of ContentsUsing Import-Csv and Export-CsvUsing + operatorUsing the File Class Using Import-Csv and Export-Csv Before merging multiple .csv files, let’s write a script to see how many .csv files are in the specified directory. For that, create a variable called $sourcefiles containing a list of all the CSV files in the directory. Next, use […]

  • Multiline command powershell
    10 January

    PowerShell – Multiline Command

    Table of ContentsUsing BackticksUsing Pipeline OperatorUsing Specific Operators/Characters Using Backticks Use backticks to split multiline commands in PowerShell. [crayon-63c429083f09b437043805/] Usually, we get an automatic line continuation in PowerShell when the command can’t syntactically be complete. However, if splitting a multiline command is required, then using backticks is preferred in such scenarios as presented above where […]

  • Call Function with Parameters in PowerShell
    08 January

    PowerShell – Call Function with Parameters

    Table of ContentsUsing Positional ParametersUsing Named Parameters Using Positional Parameters To call a function, use positional parameters separated by space. Please note that you need to pass parameters in sequence while calling the function. [crayon-63c429084645b616466567/] [crayon-63c4290846461138014948/] Microsoft’s PowerShell consists of a command-line shell and scripting language. It is based on the .NET framework for task […]

  • 08 January

    Press Any Key to Continue in PowerShell

    Table of ContentsSolutions for PowerShell Integrated Scripting Environment(ISE) and Command Line ConsoleUsing Read-Host CmdletUsing Message Box UISolutions for PowerShell Command Line ConsoleUsing ReadKey() Method with/without [void]Using RawUI.ReadKey() MethodUsing cmd /c pause CommandUsing timeout /t Command Solutions for PowerShell Integrated Scripting Environment(ISE) and Command Line Console Using Read-Host Cmdlet Use the Read-Host command to enable press […]

  • 08 January

    PowerShell – Unzip File

    Table of ContentsUsing Expand-Archive CmdletUsing System.IO.Compression.ZipFile NamespaceUsing 7Zip Module Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. [crayon-63c4290841212983558460/] Please note that Expand-Archive cmdlet is available from PowerShell 5 onwards. Print envionment variable $PSVersionTable.PSVersion to check version of your PowerShell. We used the Expand-Achrive cmdlet to unzip a folder from the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.