Practical Powershell. Today we will be using Powershell to help you with day-to-day tasks. We are going to explore how to create a Powershell script to delete old files on your hard drive.
Like I have said before, I enjoy Powershell and like how it gives me more control over my computer, particularly how it makes mundane tasks a lot more simple [and fast] for me to do. The good news is that if you don't have a lot of experience with Powershell, it is quite easy to extend and build new features using the language.
Let's get started.
Great, but how do we get all of the files on our hard drive? We can do that with the -Recurse flag (Microsoft calls these parameters but it is very common in software development to call these flags). Try running the following command:
This isn't good enough, we don't ever want to hardcode values, even in our scripts. I would think (hope) that we could get the value of our user folder programmatically, don't you? The good news is, we can with environment variables. In order to access an environment variable in Powershell, all we need to type is $env:[environmentvariablename]. The environment variable that holds the user folder is USERPROFILE, so to return the value of our user directory, we can execute this in Powershell:
You may be asking, how does that command work? Therein lies some complexity that requires explanation:
There is still room for improvement, however. I am still getting files of every single extension returned, it would be great to filter the file extensions and only delete files of types that I actively use. Fortunately, there is a built-in flag in the Get-ChildItem cmdlet that allows me to do that, -Include. See the example below:
In order to select all of the files whose LastWriteTime is older than 3 years, we can use the Where-Object command to filter the files like so:
Like I have said before, I enjoy Powershell and like how it gives me more control over my computer, particularly how it makes mundane tasks a lot more simple [and fast] for me to do. The good news is that if you don't have a lot of experience with Powershell, it is quite easy to extend and build new features using the language.
Powershell (not the official logo) |
Let's get started.
Writing the script
In order for us to delete all of the old files from our hard drive, we need to be able to somehow find all these old files. A better question to ask would be; "what qualifies a file as old?" For the purposes of this example, let's assume that any file that has not been modified in the past 3 years be marked as "old" and should be deleted. Great - now how do we delete these files in Powershell?
We will be making use of cmdlets, particularly the Get-ChildItem cmdlet. The Get-ChildItem cmdlet ("command-let") returns child items from a given location. We are going to use this to return all of the files on our hard drive. Assuming you have a C:\ drive like most (if you are on Windows), run the following command in Powershell:
Get-ChildItem -Path C:
You should have a list of files and folders that are in your C:\ directory.Getting items in your C:\ drive |
Great, but how do we get all of the files on our hard drive? We can do that with the -Recurse flag (Microsoft calls these parameters but it is very common in software development to call these flags). Try running the following command:
Get-ChildItem -Path C: -Recurse
Very likely, you will have a lot of files that will be printed after running this command. You can wait to view all of these files, which will take a very long time, or you can hit ctrl + c to stop the command from executing. But, you might have multiple users on your machine, what if you want to search for the files of the currently logged-in user? You would have to change your command to something like this:Get-ChildItem -Path C:\Users\[username] -RecurseReplacing [username] with the name of your user account on your computer.
This isn't good enough, we don't ever want to hardcode values, even in our scripts. I would think (hope) that we could get the value of our user folder programmatically, don't you? The good news is, we can with environment variables. In order to access an environment variable in Powershell, all we need to type is $env:[environmentvariablename]. The environment variable that holds the user folder is USERPROFILE, so to return the value of our user directory, we can execute this in Powershell:
$env:USERPROFILE
The current user's directory |
You may be asking, how does that command work? Therein lies some complexity that requires explanation:
- Powershell uses the colon character to denote drives. For comparison, this is how Powershell knows to look in the C:\ drive.
- Windows actually stores environment variables in it's own drive named Env.
- Powershell makes available the variable $env as an alias to the Env:\ drive.
In fact, you can list out all environment variables in Powershell by executing Get-ChildItem Env:!
Let's now pull in all files for our currently logged-in user:
Get-ChildItem -Path $env:USERPROFILE -RecurseThe reason we are using $env: instead of Env: is that $env will return for us the string value of the environment variable, instead of Env:, which returns a hash table entry for the given environment variable.
There is still room for improvement, however. I am still getting files of every single extension returned, it would be great to filter the file extensions and only delete files of types that I actively use. Fortunately, there is a built-in flag in the Get-ChildItem cmdlet that allows me to do that, -Include. See the example below:
Get-ChildItem -Path $env:USERPROFILE -Recurse -Include *.png, *.docxI can use the -Include flag to only include files that match a given extension. I can also include multiple extensions by separating them with a comma.
How do we look at old files?
I hope you didn't forget, but we still need to only pull the files that haven't been modified in over 3 years to be deleted. In order for us to do that, we need to know when each file was modified. We haven't yet seen the properties on files in Powershell, and for us to see them, we will have to use Format-List. Format-List will allow us to see all of the properties on a given file (including last modified date).
In order to use Format-List, we will have to pipe the output from Get-ChildItem to Format-List. Please see here if you want a primer on pipes. To see the output of Format-List, we can run the following command (be sure to abord the command with ctrl + c to stop it and view the output):
Get-ChildItem -Path $env:USERPROFILE -Recurse -Include *.png, *.docx | Format-List *From this output, we can see that there is a property named LastWriteTime, which is essentially our "last modified date" that we need to check against.
Look at all those details |
In order to select all of the files whose LastWriteTime is older than 3 years, we can use the Where-Object command to filter the files like so:
Get-ChildItem -Path $env:USERPROFILE -Recurse -Include *.png, *.docx | Where-Object LastWriteTime -LT (Get-Date).AddYears(-3)We compare the LastWriteTime to be less than 3 years ago using the Where-Object cmdlet.
Removing the files
Pipe that command into Remove-Item and you are set!
Get-ChildItem -Path $env:USERPROFILE -Recurse -Include *.png, *.docx | Where-Object LastWriteTime -LE (Get-Date).AddYears(-3) | Remove-Item
i am ERIC BRUNT by name. Greetings to every one that is reading this testimony. I have been rejected by my wife after three(3) years of marriage just because another Man had a spell on her and she left me and the kid to suffer. one day when i was reading through the web, i saw a post on how this spell caster on this address AKHERETEMPLE@gmail.com have help a woman to get back her husband and i gave him a reply to his address and he told me that a man had a spell on my wife and he told me that he will help me and after 3 days that i will have my wife back. i believed him and today i am glad to let you all know that this spell caster have the power to bring lovers back. because i am now happy with my wife. Thanks for helping me Dr Akhere contact him on email: AKHERETEMPLE@gmail.com
ReplyDeleteor
call/whatsapp:+2349057261346
i am ERIC BRUNT by name. Greetings to every one that is reading this testimony. I have been rejected by my wife after three(3) years of marriage just because another Man had a spell on her and she left me and the kid to suffer. one day when i was reading through the web, i saw a post on how this spell caster on this address AKHERETEMPLE@gmail.com have help a woman to get back her husband and i gave him a reply to his address and he told me that a man had a spell on my wife and he told me that he will help me and after 3 days that i will have my wife back. i believed him and today i am glad to let you all know that this spell caster have the power to bring lovers back. because i am now happy with my wife. Thanks for helping me Dr Akhere contact him on email: AKHERETEMPLE@gmail.com
or
call/whatsapp:+2349057261346