Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to delete files using PowerShell

Often, system admins, engineers, or developers might need to deploy a lot of difficult tasks with multiple schedules, triggers, or arguments. As a sysadmin, you can delete files using PowerShell, a folder, or even files older than a period of time by leveraging the PowerShell features. In this guide, we’ll show you how to complete these tasks.

PowerShell requirements

Below are the requirements in order to use PowerShell:

  • In our examples, in this article, we will use Windows 10. But Windows Server 2012 and newer versions can do the same.
  • Windows PowerShell 5.1 or PowerShell 7.
  • Script editor such as Visual Studio Code, Notepad++, or Windows PowerShell ISE.

How to delete files using PowerShell

The first example that would be most useful is the most basic – that is, deleting a single file. To Delete a file using PowerShell, you only need to use the command below. The code below deletes the file D:\temp\example.txt. We can delete a file using remove-item command as below.

Remove-Item -Path D:\temp\example.txt
  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator
  3. Type the following command and press ENTER
  4. Running the code below in PowerShell would not show anything on the screen unless an error was encountered.

How to Delete Files using PowerShell

How to delete multiple files using PowerShell

In the second example, using the command below you can delete multiple files in a folder using PowerShell. The Get-ChildItem cmdlet targets   D:\temp\ with the -Path parameter. The -File parameter indicates that the only type of item to be included are files. Get-ChildItem ignores folders.

  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator
  3. Type the following command and press Enter
Get-ChildItem -Path D:\temp\ -File | Remove-Item -Verbose

The output should look like this screenshot below:

How to delete files using PowerShell older Than x Days

In the third example, we will show you how to delete files using PowerShell older Than x Days. This example is useful for removing old log files, like those generated by IIS web servers, to free up disk space. In this example, there are files in c:\temp that are older than 492 days. Using the script below, the Name, CreationTIme, and AgeInDays of each file in c:\temp is shown.

  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator
  3. Type the following command and press ENTER
Get-ChildItem c:\temp | Select-Object Name,CreationTime,@{n='AgeInDays';e={(New-TimeSpan -Start $PSItem.CreationTime).Days}}

The output should look like this screenshot below:

How to delete files older than using PowerShell

As you can see in the screenshot below, there are files that are 456 days old and 492 days old.

Now that you know which files to remove, you can create a script to only delete files that are older than a specific number of days – in this case, older than 492 days.

In this example script below, files in C:\temp whose CreationTime value is older than the set threshold will be deleted.

The first line defines the path for Get-ChildItem to search. The path is saved in the $path variable. Then, the second line is where the threshold is specified. The value of the $threshold the age in days that the file to be deleted must be.

The next line of code after the $threshold variable will:

  • Get the collection of files located in the folder specified in the $path variable. In this example, the path is C:\temp.
  • Filter the output to include only files whose CreationTime value is older than the number of days saved in the $threshold variable. In this example, the threshold is 14 days.
  • Pipe the filtered list of files to the Remove-Item value to perform deletion of those files.
  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator
  3. Type the following command and press ENTER
$path = 'C:\Temp'
$threshold = 492
Get-ChildItem -Path $path -File | Where-Object {$PSItem.CreationTime -lt (Get-Date).AddDays(-$threshold)} |Remove-Item -Verbose

When you run the above code you will see output like shown below:

How to delete files older than X days using PowerShell

How to delete the folder using PowerShell

Remove-Item command works for deleting directories as well. In our fourth example, we will show you how to delete folders using PowerShell.

  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator
  3. Type the following commands and press ENTER
Remove-Item D:\temp\FolderToDelete

When you run the above code you will see output like shown below:

How to delete the folder using PowerShell

Note: During command execution, it will require you to confirm before proceeding. Just Pres Y.

Conclusions

We hope this guide helps you to delete files using PowerShell or even folders and files older than a specific date. You can also visit our website in order to perform more complex tasks such as: Three different ways on how to export and import scheduled tasks or How to create PowerShell scheduled task etc.

The post How to delete files using PowerShell appeared first on Get IT Solutions.



This post first appeared on Get It Solutions, please read the originial post: here

Share the post

How to delete files using PowerShell

×

Subscribe to Get It Solutions

Get updates delivered right to your inbox!

Thank you for your subscription

×