Skip to main content

PowerShell Scripting: Removing Multiple Directories

Update 20150916
So the pic I used to display the CSV sample went missing. So the latest update just includes a text version of what was displayed. Thanks!

Original Post
I'm still fairly new to utilizing PowerShell to automate tasks and get big tasks done. But every time I use it, the tool makes my life a whole lot easier. Recently I was tasked with deleting over 500 directories. Thankfully someone else had the task of identifying the directories that were slated to be deleted, I just had to complete the process of removing them. Thanks to a little article from the Scripting Guy! Blog (found HERE) I had some direction to start in.

What I ended up doing is creating a single column CSV file that contains the path to the directories to be deleted. This path includes drive letter, and the overall path to the folder. It should look similar to this:
delete
C:\Path\to\folder
C:\Path\to\folder2
D:\Path\to\folder
D:\Path\to\folder2

I named the file "delete.csv" and placed it into my scripting folder on the C drive. Now with this set up and in place we can move onto using PowerShell. We will be using a combination of commands in a one-liner that will not only delete the files but auto-confirm the deletion. I'll put the command next and attempt to break it down as far as my knowledge of PowerShell goes...

Import-CSV .\delete.csv | Foreach-Object { Remove-Item -LiteralPath $_.delete -Force -Recurse -WhatIf }

The first part "Import-CSV" obviously tells PowerShell to look at the CSV we created with the necessary directories to be deleted. The "Foreach-Object" will loop through the CSV to delete all items in it. The "Remove-Item" part tells PowerShell to deleted the specified items with a "LiteralPath" (where the directory can be found) found in the "delete" column. The "Force" item tells PowerShell to remove items that cannot be changed, such as read only or hidden files. The "Recurse" item will force the confirmation of the deletion (so you don't have to babysit the command and keep hitting "Y"). Finally the "WhatIf" item allows you to test the command and see if it works before running it. This also gives you an opportunity to make sure you have the correct directories that you want to delete. Be sure to remove the "-WhatIf" when you are ready for the command to delete the selected directories!!

For more information check out the Microsoft Tech Net article on the Remove-Item cmdlet HERE.

Comments

  1. Larry,

    Thanks much. This worked great. You may want to make available a CSV sample to help in setting that file up. Even without that file (though), I was able to clean up some folders that had a specific criteria.

    Great Work,
    Greg R

    ReplyDelete
    Replies
    1. Hi Greg,

      Thanks for pointing that out! I had a screen shot there of the sample CSV that seems to have been lost... I've updated the post with some sample text of what the CSV should contain. Glad it worked for you.

      Delete

Post a Comment

Popular posts from this blog

Using Python for GPG/PGP File Encryption - Part 1

So, this will be the start of a series that will build a python script for GPG/PGP file encryption. In this post, we'll look at installing gnupg for python and using python to setup the keystore, create a private key, exporting the associated public key, and importing a public key. Now everything done here can be done with simple gnupg commands, but learning how to do this with python will help in understanding the script we'll be building to complete file encryption. I will be covering non-python gnupg commands in a future post. Additionally, the folks at the python-gnupg site over at pythonhosted.org have done a really great job at documenting everything (link to their site at the bottom). The stuff I'll be going over will be more of a start-to-finish for anyone that may get lost in the muck of doing stuff with python. Full Disclosure #1: Any key identifier throughout the series of posts is FICTITIOUS and DOES NOT represent any real key, either associated with myself or...

Windows Server 2008: Log on as batch job

From time to time, I have to set up some scheduled tasks that required a dedicated account to run. And when doing so, I'll usually forget that the dedicated account usually isn't given any more permissions than what it needs to complete the task at hand. So, after setting up the task, Windows will usually yell at me and say "The account needs batch job rights". So here's how to grant batch job permissions on your server. Go to your start menu, and start searching for Local Security Policy In the left pane of the MMC that opens up, expand Local Policies, and highlight User Rights Assignment. Now, in the left right pane, locate "Log on as a batch job" and double click it. In the properties window that opens up, add the user or group that needs this permission. I find that if you have multiple service accounts running different tasks on the same server, it's easier to just add a group verses the individual a...

Replacing rsyslog with syslog-ng on RHEL 6.5

So...I had a piece of monitoring software that didn't play nice with the RHEL default rsyslog for log collection. The software was developed to only work with syslog-ng. I'll be going over the steps that I took, that worked for me, in replacing rsyslog with syslog-ng. I would imagine that these same steps should work for any Linux system similar to RHEL (Fedora, CentOS, etc.). For others (like Debein based distributions), I would need to look into that (coming in a future update to this post). First, remove rsyslog. You will need to keep the dependencies as they will be needed for syslog-ng: sudo rpm -e --nodeps rsyslog Next we will need to add the EPEL repository (more info can be found HERE ): wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm sudo rpm -ivh epel-release-6-8.rpm sudo yum repolist That last command will list all the installed repositories. You are simply verifying that the EPEL package has been installed. Now that we fi...