{"id":8093,"date":"2023-04-04T14:34:20","date_gmt":"2023-04-04T12:34:20","guid":{"rendered":"https:\/\/lazyadmin.nl\/?p=8093"},"modified":"2024-02-21T14:38:57","modified_gmt":"2024-02-21T12:38:57","slug":"powershell-delete-file","status":"publish","type":"post","link":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/","title":{"rendered":"How to Delete a File with PowerShell Remove-Item"},"content":{"rendered":"\n<p>Deleting files and cleaning up directories can be a time-consuming task, especially when you need to perform the same task on multiple computers regularly. But with PowerShell, we can create small scripts that will delete a file quickly and efficiently by using the Remove-Item cmdlet.<\/p>\n\n\n\n<p>PowerShell allows us not to only delete a single file, but even an entire directory or a group of files based on specific criteria. And did you know it&#8217;s also possible to only delete a file if it exists, files that are older than x days, or delete files based on a wildcard pattern?<\/p>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">In this article<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f31673d56a2\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\" style='display:none' id=\"ez-toc-cssicon-toggle-item-69f31673d56a2\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#delete-a-file-with-powershell\" >Delete a file with PowerShell<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#delete-all-files-in-a-folder\" >Delete all Files in a Folder<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#remove-files-in-folder-and-subfolders\" >Remove Files in Folder and SubFolders<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#delete-read-only-and-hidden-files\" >Delete Read-Only and Hidden Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#powershell-delete-file-if-exists\" >PowerShell Delete File if Exists<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#using-filters-to-delete-files-in-powershell\" >Using Filters to Delete Files in PowerShell<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#using-the-include-filter\" >Using the Include Filter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#using-the-exclude-filter\" >Using the Exclude Filter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#delete-files-older-than-x-days-with-powershell\" >Delete Files Older than x Days with PowerShell<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#delete-files-larger-than\" >Delete Files Larger Than<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#wrapping-up\" >Wrapping Up<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<p>In this article, we are going to take a look at how to delete a file in PowerShell. And how to use the different options to delete only specific files, verify the delete action, and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"delete-a-file-with-powershell\"><\/span>Delete a file with PowerShell<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To delete files with PowerShell we need to use the Remove-Item cmdlet. This cmdlet can delete one or more items based on the criteria. The Remove-Item cmdlet can not only be used to delete files but also for deleting folders, registry keys, variables, functions, and more. We will focus on deleting files in this article.<\/p>\n\n\n\n<p>With the Remove-Item cmdlet, we can use the following parameters:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th class=\"has-text-align-left\" data-align=\"left\"><strong>Parameter<\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">-Path<\/td><td class=\"has-text-align-left\" data-align=\"left\">Specify the path of the items that need to be removed.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-LiteralPath<\/td><td class=\"has-text-align-left\" data-align=\"left\">The exact path the item<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-Include<\/td><td class=\"has-text-align-left\" data-align=\"left\">Specifies a path element to include, for example, *.txt<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-Exclude<\/td><td class=\"has-text-align-left\" data-align=\"left\">Specifies a path element to exclude, for example, *.txt<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-Recurse<\/td><td class=\"has-text-align-left\" data-align=\"left\">Delete items in the specified location and in all subfolders<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-Force<\/td><td class=\"has-text-align-left\" data-align=\"left\">Used to delete read-only files or hidden items<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">-Confirm<\/td><td class=\"has-text-align-left\" data-align=\"left\">Prompt for confirmation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So to delete a single file with PowerShell we only need to specify the full path to the file including the file name. For example, if we want to delete the readme.txt file from the folder below we can do:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Remove-Item -Path C:\\temp\\files\\readme.txt<\/pre>\n\n\n\n<p>Note that the cmdlet won&#8217;t ask for confirmation, the file will be deleted instantly. So when you are writing your PowerShell script that is going to delete one or more files, it&#8217;s important to test it first. We can do this by using either the <code>-confirm<\/code> or <code>-whatif<\/code> parameter. Confirm will give a prompt if you want to delete the file or not, where what if shows what the cmdlet would do without actually deleting the item:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Using Confirm\nRemove-Item -Path C:\\temp\\files\\readme.txt -Confirm\n\nConfirm\nAre you sure you want to perform this action?\nPerforming the operation \"Remove File\" on target \"C:\\temp\\files\\readme.txt\".\n[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is \"Y\"): \n\n# Or using WhatIf\nRemove-Item -Path C:\\temp\\files\\readme.txt -whatif\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\readme.txt\".<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"809\" height=\"281\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image.png\" alt=\"powershell remove file\" class=\"wp-image-8094\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image.png 809w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-300x104.png 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-768x267.png 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-800x278.png 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-400x139.png 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-50x17.png 50w\" sizes=\"(max-width: 809px) 100vw, 809px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"delete-all-files-in-a-folder\"><\/span>Delete all Files in a Folder<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can use the <code>Remove-Item<\/code> cmdlet in PowerShell also to delete all files in a folder. Now important to note here is that the cmdlet can also remove folders.  So to delete <strong>only the files<\/strong>, we are going to use a wildcard to select only the items that include a dot <code>.<\/code> in the name. This way only files are deleted and not any subfolders:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Remove all files from the path c:\\temp\\files\n# Tip: test the results first with the -whatif parameter\nRemove-Item -Path C:\\temp\\files\\*.*<\/pre>\n\n\n\n<p>Another option is to use the cmdlet <code>Get-ChildItem<\/code> with the parameter <code>-File<\/code> to get only the files and then pipe the <code>Remove-Item<\/code> cmdlet to remove the files with PowerShell:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-ChildItem -Path C:\\temp\\files\\ -File | Remove-Item<\/pre>\n\n\n<script async src=\"https:\/\/alatus.eocampaign1.com\/form\/4217287c-b05c-11ee-a0ac-e5852477572a.js\" data-form=\"4217287c-b05c-11ee-a0ac-e5852477572a\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"remove-files-in-folder-and-subfolders\"><\/span>Remove Files in Folder and SubFolders<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To delete files not only in the folder but in the subfolder as well, you will need to use the -Recurse parameter. There is only one problem, you can&#8217;t use -Recurse recombination with the files-only selection method <code>*.*<\/code>. Take the example below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Remove-Item -Path C:\\temp\\files\\*.* -Recurse<\/pre>\n\n\n\n<p>You probably expect that this will delete only the files in the folder and subfolder. However, by using the wildcard in the path, only items (files) that contain an <code>.<\/code> in the path name are selected. So the cmdlet won&#8217;t go through any subfolders.<\/p>\n\n\n\n<p>To remove all the files in the folder and all subfolders we will have to specify only the path of the parent folder and use the <code>-Include<\/code> parameter to select only the files:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Delete all files from the folder c:\\temp\\files and it's subfolders\nRemove-Item -Path C:\\temp\\files\\ -Recurse -Include *.*<\/pre>\n\n\n\n<p>Another option is to first get the files from the folder and subfolder with the <code>Get-ChildIte<\/code>m cmdlet and then remove them:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-ChildItem -Path C:\\temp\\files\\ -File -Recurse | Remove-Item<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"delete-read-only-and-hidden-files\"><\/span>Delete Read-Only and Hidden Files<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Read-Only and hidden files are protected from deletion by default. Let&#8217;s take a look at the example below, the file &#8220;la-srv-dc01.log&#8221; is marked read-only and the template.html is a hidden file. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"809\" height=\"476\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1.png\" alt=\"powershell delete file\" class=\"wp-image-8095\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1.png 809w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1-300x177.png 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1-768x452.png 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1-800x471.png 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1-400x235.png 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/image-1-50x29.png 50w\" sizes=\"(max-width: 809px) 100vw, 809px\" \/><\/figure>\n\n\n\n<p>If we would try to delete all the files in the folder, then you will get an error on the read-only file that you do not have sufficient access rights to perform this operation. The hidden file isn&#8217;t even deleted at all. <\/p>\n\n\n\n<p>So how do we delete those files? To delete the read-only and hidden files as well we will need to use the <code>-Force<\/code> parameter:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Remove all files, including read-only and hidden\nRemove-Item -Path C:\\temp\\files\\*.* -Force<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"powershell-delete-file-if-exists\"><\/span>PowerShell Delete File if Exists<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When using the Remove-Item cmdlet inside scripts, it&#8217;s a good idea to test if the file exists before you try to delete it. We can do this by using the <a href=\"https:\/\/lazyadmin.nl\/it\/test-path\/\">Test-Path cmdlet<\/a>. This will check if the file exists and return true or false based on the result. By using this inside an if statement we can make sure that the file is only deleted if it exists:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$file = C:\\temp\\files\\readme.txt\n\nif (Test-Path -Path $file) {\n    Remove-Item -Path $file\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"using-filters-to-delete-files-in-powershell\"><\/span>Using Filters to Delete Files in PowerShell<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We now have looked at the some of principles to delete files with PowerShell. But besides simply deleting a single file or all the files in the folder, you will often encounter situations where you only need to delete files older than x days, or files with a specific file type.<\/p>\n\n\n\n<p>We can use a variety of methods to select only those files and delete them with PowerShell. When working with filters always make sure that you first test and verify the selection, before you actually delete the files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"using-the-include-filter\"><\/span>Using the Include Filter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Include parameter allows us to select all files that have a specific string or part of a string in its file name. For example, if you only want to remove the .log files from the folder, you can do:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Remove all .log items\nRemove-Item -Path C:\\temp\\files\\ -Include *.log<\/pre>\n\n\n\n<p>The include parameter isn&#8217;t limited to file extensions, you can also select files based on part of their filename. And it&#8217;s also possible to specify multiple &#8220;keywords&#8221; that you want to select:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Get all files that have the word app or process in their name.\nRemove-Item -Path C:\\temp\\files\\ -Include *app*, *process* -Recurse -WhatIf\n\n# Result\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\subfolder\\LT3452-process-errors.log\".\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\subfolder\\LT3452-process.log\".\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\11032021-app.log\".\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\11032022-app.log\".\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"using-the-exclude-filter\"><\/span>Using the Exclude Filter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Following the principle of the include parameter, we can also exclude files. Include and exclude can be used together, so you could for example remove all log files except the process log files:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Remove-Item -Path C:\\temp\\files\\ -Include *.log -Exclude *process* -Recurse -WhatIf\n\n# Result\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\11032021-app.log\".\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\11032022-app.log\".\nWhat if: Performing the operation \"Remove File\" on target \"C:\\temp\\files\\la-srv-dc01.log\".<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"delete-files-older-than-x-days-with-powershell\"><\/span>Delete Files Older than x Days with PowerShell<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A common practice when cleaning up directories is to delete only files that are older than x days. To do this, we will first need to calculate the file date. let&#8217;s say we want to delete all files that are older than 30 days, we then first need to get the date from 30 days ago:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Get the date from today minus 30 days\n$dateTime = (Get-Date).AddDays(-30)<\/pre>\n\n\n\n<p>The next step is to get all files that are older than the given date. To do this we are going to compare the <code>lastwritetime<\/code> of the file with the <code>datetime<\/code> that we calculated:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $dateTime }<\/pre>\n\n\n\n<p>We then only have to pipe the <code>Remove-Item<\/code> cmdlet behind it to actually delete the files:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$dateTime = (Get-Date).AddDays(-30)\n$path = \"C:\\temp\\files\\\"\n\nGet-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $dateTime } | Remove-Item<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"delete-files-larger-than\"><\/span>Delete Files Larger Than<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To delete all files that are larger than a given size, we will first need to convert the file size from Mb to kbits. With the correct size, we can then select all files that are larger than the givne size and remove them with the <code>Remove-Item<\/code> cmdlet:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$path = \"C:\\temp\\files\\\"\n\n# Set file size\n$sizeInMb = 500\n\n# Calculate actual file size\n$size = $sizeInMb*1024*1024\n\n# Delete all files that are larger then given size\nGet-ChildItem -Path $Path -Recurse -File | Where-Object { $_.length -gt $size } | Remove-Item<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"wrapping-up\"><\/span>Wrapping Up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>PowerShell is a great tool when it comes to managing and deleting files automatically. You can write scripts that will clean up old files every week to quickly find and delete large files from the given folder.<\/p>\n\n\n\n<p>Keep in mind that files are deleted permanently, so make sure that you test your commands first using the <code> <\/code>parameter before you actually delete the files.<\/p>\n\n\n\n<p>I hope you found this article useful, if you have any questions, just drop a comment below.<\/p>\n<script async src=\"https:\/\/alatus.eocampaign1.com\/form\/d0ce5de8-9b48-11ee-8cf4-eb425eea7369.js\" data-form=\"d0ce5de8-9b48-11ee-8cf4-eb425eea7369\"><\/script>","protected":false},"excerpt":{"rendered":"<p>Deleting files and cleaning up directories can be a time-consuming task, especially when you need to perform the same task on multiple computers regularly. But with PowerShell, we can create small scripts that will delete a file quickly and efficiently &#8230; <a title=\"How to Delete a File with PowerShell Remove-Item\" class=\"read-more\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\">Read more<span class=\"screen-reader-text\">How to Delete a File with PowerShell Remove-Item<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":8098,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[69],"tags":[22],"class_list":["post-8093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-powershell"],"wppr_data":{"cwp_meta_box_check":"No"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin<\/title>\n<meta name=\"description\" content=\"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin\" \/>\n<meta property=\"og:description\" content=\"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\" \/>\n<meta property=\"og:site_name\" content=\"LazyAdmin\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/lazyadminnl\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/lazyadminnl\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-04T12:34:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-21T12:38:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rudy Mens\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/lazyadmin\" \/>\n<meta name=\"twitter:site\" content=\"@lazyadmin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy Mens\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\"},\"author\":{\"name\":\"Rudy Mens\",\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"headline\":\"How to Delete a File with PowerShell Remove-Item\",\"datePublished\":\"2023-04-04T12:34:20+00:00\",\"dateModified\":\"2024-02-21T12:38:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\"},\"wordCount\":1278,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"image\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg\",\"keywords\":[\"Powershell\"],\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\",\"url\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\",\"name\":\"How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin\",\"isPartOf\":{\"@id\":\"https:\/\/lazyadmin.nl\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg\",\"datePublished\":\"2023-04-04T12:34:20+00:00\",\"dateModified\":\"2024-02-21T12:38:57+00:00\",\"description\":\"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.\",\"breadcrumb\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage\",\"url\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg\",\"contentUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg\",\"width\":800,\"height\":450},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/lazyadmin.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Delete a File with PowerShell Remove-Item\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/lazyadmin.nl\/#website\",\"url\":\"https:\/\/lazyadmin.nl\/\",\"name\":\"LazyAdmin\",\"description\":\"Tips and howto&#039;s about Office 365, PowerShell, Home network and smart devices\",\"publisher\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/lazyadmin.nl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\",\"name\":\"Rudy Mens\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg\",\"contentUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg\",\"width\":500,\"height\":500,\"caption\":\"Rudy Mens\"},\"logo\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/\"},\"description\":\"Ruud worked for more than 15 years as a SysAdmin in the Netherlands and is now working as an independent consultant. In his spare time, he loves to thinker with Smart Devices.\",\"sameAs\":[\"https:\/\/lazyadmin.nl\",\"https:\/\/www.facebook.com\/lazyadminnl\/\",\"http:\/\/nl.linkedin.com\/in\/rudymens\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/lazyadmin\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin","description":"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/","og_locale":"en_US","og_type":"article","og_title":"How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin","og_description":"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.","og_url":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/","og_site_name":"LazyAdmin","article_publisher":"https:\/\/www.facebook.com\/lazyadminnl\/","article_author":"https:\/\/www.facebook.com\/lazyadminnl\/","article_published_time":"2023-04-04T12:34:20+00:00","article_modified_time":"2024-02-21T12:38:57+00:00","og_image":[{"width":800,"height":450,"url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg","type":"image\/jpeg"}],"author":"Rudy Mens","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/lazyadmin","twitter_site":"@lazyadmin","twitter_misc":{"Written by":"Rudy Mens","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#article","isPartOf":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/"},"author":{"name":"Rudy Mens","@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"headline":"How to Delete a File with PowerShell Remove-Item","datePublished":"2023-04-04T12:34:20+00:00","dateModified":"2024-02-21T12:38:57+00:00","mainEntityOfPage":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/"},"wordCount":1278,"commentCount":0,"publisher":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"image":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage"},"thumbnailUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg","keywords":["Powershell"],"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/","url":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/","name":"How to Delete a File with PowerShell Remove-Item &#8212; LazyAdmin","isPartOf":{"@id":"https:\/\/lazyadmin.nl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage"},"image":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage"},"thumbnailUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg","datePublished":"2023-04-04T12:34:20+00:00","dateModified":"2024-02-21T12:38:57+00:00","description":"Learn how to delete files with PowerShell using different selection methods, based on file size, age or filter on (part of) filename.","breadcrumb":{"@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#primaryimage","url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg","contentUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg","width":800,"height":450},{"@type":"BreadcrumbList","@id":"https:\/\/lazyadmin.nl\/powershell\/powershell-delete-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/lazyadmin.nl\/"},{"@type":"ListItem","position":2,"name":"How to Delete a File with PowerShell Remove-Item"}]},{"@type":"WebSite","@id":"https:\/\/lazyadmin.nl\/#website","url":"https:\/\/lazyadmin.nl\/","name":"LazyAdmin","description":"Tips and howto&#039;s about Office 365, PowerShell, Home network and smart devices","publisher":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lazyadmin.nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952","name":"Rudy Mens","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/","url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg","contentUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg","width":500,"height":500,"caption":"Rudy Mens"},"logo":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/"},"description":"Ruud worked for more than 15 years as a SysAdmin in the Netherlands and is now working as an independent consultant. In his spare time, he loves to thinker with Smart Devices.","sameAs":["https:\/\/lazyadmin.nl","https:\/\/www.facebook.com\/lazyadminnl\/","http:\/\/nl.linkedin.com\/in\/rudymens\/","https:\/\/x.com\/https:\/\/twitter.com\/lazyadmin"]}]}},"uagb_featured_image_src":{"full":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",800,450,false],"thumbnail":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files-150x150.jpg",150,150,true],"medium":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files-300x169.jpg",300,169,true],"medium_large":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files-768x432.jpg",768,432,true],"large":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",800,450,false],"1536x1536":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",800,450,false],"2048x2048":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",800,450,false],"post-thumb":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",800,450,false],"post-thumb-half":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files-400x225.jpg",400,225,true],"wppr-widget":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files-50x28.jpg",50,28,true],"gform-image-choice-sm":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",300,169,false],"gform-image-choice-md":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",400,225,false],"gform-image-choice-lg":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/04\/powershell-delete-files.jpg",600,338,false]},"uagb_author_info":{"display_name":"Rudy Mens","author_link":"https:\/\/lazyadmin.nl\/author\/lajcud8123b\/"},"uagb_comment_info":0,"uagb_excerpt":"Deleting files and cleaning up directories can be a time-consuming task, especially when you need to perform the same task on multiple computers regularly. But with PowerShell, we can create small scripts that will delete a file quickly and efficiently ... Read moreHow to Delete a File with PowerShell Remove-Item","_links":{"self":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts\/8093"}],"collection":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/comments?post=8093"}],"version-history":[{"count":0,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts\/8093\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/media\/8098"}],"wp:attachment":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/media?parent=8093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/categories?post=8093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/tags?post=8093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}