{"id":2041,"date":"2019-10-29T10:47:57","date_gmt":"2019-10-29T10:47:57","guid":{"rendered":"https:\/\/www.stcleaner.com\/blog\/?p=2041"},"modified":"2019-10-29T10:50:07","modified_gmt":"2019-10-29T10:50:07","slug":"how-to-use-powershell-to-delete-files","status":"publish","type":"post","link":"https:\/\/www.stcleaner.com\/blog\/2019\/10\/29\/how-to-use-powershell-to-delete-files\/","title":{"rendered":"How to Use PowerShell to Delete Files?"},"content":{"rendered":"<p><b>Windows PowerShell<\/b><span style=\"font-weight: 400;\"> is a command-line utility from <\/span><b>Microsoft<\/b><span style=\"font-weight: 400;\">. It works similar to <\/span><b>Command Prompt<\/b><span style=\"font-weight: 400;\"> and comes pre-installed with the <\/span><b>Windows 10<\/b><span style=\"font-weight: 400;\">. PowerShell allows users to perform several administrative tasks and change system settings &amp; configurations. It is an automatic task framework that enables administrators to perform batch processing,<\/span><b> locally<\/b><span style=\"font-weight: 400;\">, and <\/span><b>remotely <\/b><span style=\"font-weight: 400;\">with the use of technologies like <\/span><b>COM<\/b><span style=\"font-weight: 400;\"> and <\/span><b>WMI<\/b><span style=\"font-weight: 400;\">. PowerShell can execute scripts, functions, commands, and applications.<\/span><\/p>\n<p><!--more--><\/p>\n<p>\u00a0<\/p>\n<h2><b>Delete a Directory or File<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">To delete a single directory or a file from your hard disk, open up PowerShell and then use the commands given below. Hit the <\/span><b>Enter<\/b><span style=\"font-weight: 400;\"> key to execute the command.<\/span><\/p>\n<h3><b>Open PowerShell<\/b><\/h3>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Click the <\/span><b>Start <\/b><span style=\"font-weight: 400;\">button and type <\/span><b>PowerShell<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Right-click <\/span><b>Windows PowerShell<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Select <\/span><b>Run as administrator<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Click <\/span><b>Yes<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p>\u00a0<\/p>\n<h3><b>Deleting a Directory<\/b><\/h3>\n<ul>\n<li>Remove-Item -path D:\\Hello -recurse -force<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In this command, replace <\/span><span style=\"color: #800000;\"><b>D:\\Hello<\/b><\/span><span style=\"font-weight: 400;\"> with the directory name you want to delete.<\/span><\/p>\n<p>\u00a0<\/p>\n<h3><b>Deleting a File<\/b><\/h3>\n<ul>\n<li>Remove-Item -path D:\\Hello\\Alex.pdf -recurse -force<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In this command, replace <\/span><span style=\"color: #800000;\"><b>Alex.pdf<\/b><\/span><span style=\"font-weight: 400;\"> with the name of the file you want to delete.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><b>recurse<\/b><span style=\"font-weight: 400;\"> switch will automatically delete any subfolders without asking for your permission, and the <\/span><b>force<\/b><span style=\"font-weight: 400;\"> switch will automatically delete any <\/span><b>read-only<\/b><span style=\"font-weight: 400;\"> and <\/span><b>hidden files<\/b><span style=\"font-weight: 400;\"> without your consent.<\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><b><i>Note:<\/i><\/b><\/span><span style=\"font-weight: 400;\"> Don&#8217;t blindly copy-paste\/ write commands into the PowerShell window. You might end up accidentally deleting your crucial files. To avoid data loss, verify the commands before executing them. We strongly recommend you back up your files first to an external storage medium such as a <\/span><b>USB Stick <\/b><span style=\"font-weight: 400;\">or an <\/span><b>External Hard Disk<\/b><span style=\"font-weight: 400;\">, and then disconnect\/ unplug the device.<\/span><\/p>\n<p>\u00a0<\/p>\n<h2><b>Delete Specific File Types<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">If you need to delete specific file types from the top-most directory, use the following command:<\/span><\/p>\n<ul>\n<li>Remove-Item -path D:\\Hello remove-item * -include *.pdf<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In this command, replace <\/span><span style=\"color: #800000;\"><b>D:\\Hello<\/b><\/span><span style=\"font-weight: 400;\"> with your desired directory path and<\/span> <span style=\"color: #000000;\"><b>.pdf<\/b><span style=\"font-weight: 400;\"> with the extension of the file you want to delete.<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">To delete specific file types from all the subfolders inside a directory, add the<\/span> <b>-recurse<\/b><\/span><span style=\"font-weight: 400;\"><span style=\"color: #000000;\"> switch to your command. Now it sho<\/span>uld look like:<\/span><\/p>\n<ul>\n<li>Remove-Item -path D:\\Hello remove-item * -include *.pdf -recurse<\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<h2><b>Delete Files that Contain Specific Text<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The following command will delete all files that contain a specific word (<\/span><span style=\"color: #800000;\"><b>office<\/b><\/span><span style=\"font-weight: 400;\">, in this case).<\/span><\/p>\n<ul>\n<li>Remove-Item -path D:\\Hello * -Filter *office* -whatif<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Remember that the <\/span><b>-whatif<\/b><span style=\"font-weight: 400;\"> switch will display all the results in front of your screen so that you can review and evaluate the files.<\/span><\/p>\n<p>\u00a0<\/p>\n<h2><b>Delete Multiple Files<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">You can use PowerShell to delete as many files as you want. Simply add multiple files in a pattern separated by the comma. Here&#8217;s how:<\/span><\/p>\n<ul>\n<li>Remove-Item D:\\Hello\\USA.pdf, D:\\Hello\\Canada.pdf, D:\\Hello\\France.pdf<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In this command, PowerShell will delete <\/span><span style=\"color: #800000;\"><b>USA.pdf<\/b><\/span><b>, <\/b><span style=\"color: #800000;\"><b>Canada.pdf<\/b><\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"color: #800000;\"><b>France.pdf<\/b><\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p>\u00a0<\/p>\n<h2><b>Find and Get Duplicate Files List<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">If you have amassed loads of duplicate files on your hard disk, you can use PowerShell to generate a list of all the duplicate files. Use the command given below to do so:<\/span><\/p>\n<ul>\n<li><strong>cd D:\\DuplicateFiles<\/strong><\/li>\n<li><strong>ls *.* -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | echo<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Replace <\/span><span style=\"color: #800000;\"><b>D:\\DuplicateFiles<\/b><\/span><span style=\"font-weight: 400;\"> with the directory you want to search.<\/span><\/p>\n<figure id=\"attachment_2042\" class=\"thumbnail wp-caption aligncenter\" style=\"width: 869px\"><a href=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2042 size-full\" src=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image1-1.png\" alt=\"\" width=\"859\" height=\"453\" srcset=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image1-1.png 859w, https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image1-1-300x158.png 300w, https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image1-1-768x405.png 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/a><figcaption class=\"caption wp-caption-text\"><strong>PowerShell at work<\/strong><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<p><span style=\"font-weight: 400;\">PowerShell might take a long time to complete this entire job. It is safe to minimize PowerShell and use your computer while this job is going on in the background.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The resulting screen would be as follows. PowerShell will display a long list of your files along with their <\/span><b>path<\/b><span style=\"font-weight: 400;\">, <\/span><b>algorithm used <\/b><span style=\"font-weight: 400;\">and <\/span><b>hash value<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><a href=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2043\" src=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1.png\" alt=\"\" width=\"1366\" height=\"730\" srcset=\"https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1.png 1366w, https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1-300x160.png 300w, https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1-768x410.png 768w, https:\/\/www.stcleaner.com\/blog\/wp-content\/uploads\/2019\/10\/image2-1-1024x547.png 1024w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3><b>How Does it Work?<\/b><\/h3>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>CD<\/b><span style=\"font-weight: 400;\"> command changes\/ sets the current directory<\/span> <span style=\"font-weight: 400;\">to your desired directory.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>ls *.* -recurse<\/b><span style=\"font-weight: 400;\"> switch builds a list of <\/span><b>all files <\/b><span style=\"font-weight: 400;\">that exist in the above specified directory, including all of its <\/span><b>subfolders<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>get-filehash<\/b><span style=\"font-weight: 400;\"> switch calculates the hashes of all files in a <\/span><b>fixed sized bit string <\/b><span style=\"font-weight: 400;\">format. That is, your original <\/span><b>large dataset <\/b><span style=\"font-weight: 400;\">is converted into a far shorter set of character combinations that represent the original string. Remember, two unique files can never have the same hash value but two <\/span><b>duplicates <\/b><span style=\"font-weight: 400;\">will have the same hash value. PowerShell uses the <\/span><b>SHA-256 <\/b><span style=\"font-weight: 400;\">hash algorithm which is one of the most reliable and secure <\/span><b>one-way cryptographic <\/b><span style=\"font-weight: 400;\">32-byte hash algorithms.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Based on the above results, <\/span><b>group -property hash<\/b><span style=\"font-weight: 400;\"> switch will group files with the common hashes together.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>where { $_.count -gt 1 }<\/b><span style=\"font-weight: 400;\"> switch will select groups that contain more than <\/span><b>1 item<\/b><span style=\"font-weight: 400;\">. This automatically excludes all unique files and selects only duplicate ones.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>% { $_.group | select -skip 1 } <\/b><span style=\"font-weight: 400;\">\u00a0switch <\/span><b>skips the first file <\/b><span style=\"font-weight: 400;\">in each group to retain the original file and selects only duplicate versions.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Echo<\/b><span style=\"font-weight: 400;\"> will print\/ display a list of all the selected duplicate files in a three column-based format into PowerShell window.<\/span><\/li>\n<\/ol>\n<p>\u00a0<\/p>\n<h2><b>Find and Delete Duplicate Files At Once<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Once you have reviewed the list of all duplicate files, you can safely delete all duplicates at once using the following command given below:<\/span><\/p>\n<ul>\n<li><strong>cd D:\\DuplicateFiles<\/strong><\/li>\n<li><strong>ls *.* -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | del<\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Windows PowerShell is a command-line utility from Microsoft. It works similar to Command Prompt and comes pre-installed with the Windows 10. PowerShell allows users to perform several administrative tasks and change system settings &amp; configurations. It is an automatic task framework that enables administrators to perform batch processing, locally, and remotely with the use of [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[1094,1093,1097,1098,1095,1096,1099,1102,1101,1100,1092],"class_list":["post-2041","post","type-post","status-publish","format-standard","hentry","category-solutions","tag-use-windows-powershell-to-delete-a-file","tag-use-windows-powershell-to-delete-an-entire-directory","tag-use-windows-powershell-to-delete-files-that-contain-specific-text","tag-use-windows-powershell-to-delete-multiple-files-in-1-go","tag-use-windows-powershell-to-delete-specific-file-types","tag-use-windows-powershell-to-delete-specific-kind-of-files","tag-use-windows-powershell-to-find-duplicate-files","tag-use-windows-powershell-to-get-rid-of-delete-files","tag-use-windows-powershell-to-search-and-delete-duplicate-files","tag-use-windows-powershell-to-search-for-duplicate-files","tag-windows-powershell-usage"],"_links":{"self":[{"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":3,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2046,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2046"}],"wp:attachment":[{"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stcleaner.com\/blog\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}