Who doesn’t want to be a wizard and say a few words for magical things to happen? Well, we can’t show you how to become a wizard, but we can show you how to do a little computer magic.
If you have a Windows computer running Windows 7, you have PowerShell. Also, if you have a Windows 7 PC, update it for security.
But this article isn’t just for Windows users. The latest version of PowerShell is free and can be installed and used on both Mac and Linux computers.
This is important because what you’ve learned about PowerShell can now be used on almost any computer. Who knows? You can take this skill to the next level and pursue a career in information technology.
What is PowerShell?
Microsoft’s hi-tech answer is that it is ” a command line shell designed specifically for system administrators.” Sounds intimidating. But this is not the case Do you care about your computer? Yes, then you are the system administrator in your home.
Are you telling your computer what to do with clicks and keystrokes? Yes, so think of the command line shell as just another window where you enter information to tell the computer what to do. Can you do it.
– /
PowerShell is similar to a programming language, but not as cryptic as most others. It is indeed very similar to regular English, which Microsoft has been aiming for for non-programmers to master.
It’s a way to write multiple lines of commands called a script to make the Windows operating system do what you want it to do. Then you can save these lines to a file and run it with one click, or schedule it to run periodically.
What can you do with PowerShell?
The key to PowerShell is the ability to automate tasks so that you don’t have to waste time on your day to day activities. For a professional sysadmin, this can be something like creating new users, generating passwords for them, and sending an email with details to their new boss.
Manually, using clicks and typing, this process can take from 5 minutes to an hour or more. With the right scripts in place, the sysadmin might not even have to do any part of this.
But you want to know what you can do with PowerShell at home. Pretty much anything you don’t like doing over and over. Use it to free up hard drive space by deleting temporary files and log files that you don’t need.
Set a curfew on your child’s computer. Rename or organize a bunch of files. That’s the beauty of PowerShell. Pretty much anything you can do on your computer you can create with a PowerShell script to automate and run with one click or on a schedule.
How do I use PowerShell?
The easiest way to work with PowerShell is with the PowerShell Integrated Scripting Environment (ISE). You can find it by clicking Start and typing powershell ise in the Windows 10 search bar. You should see it as shown below.
The first time we use it, we will run it as administrator. To do this, you must first have administrator rights on your computer. From the Start menu, right-click PowerShell ISE, then select Run as administrator.
You may receive a User Access Control (UAC) warning popup asking if you really want to do this. Click Yes.
You are now looking at the PowerShell IDE. The top pane of the window (1) is the script pane. This is where you write your script. The bottom pane of the window (2) is the console area. When you test your script, you will see the result in this panel.
You will also see error messages here to help you fix and improve your scripts. The panel on the right side of the window (3) is a command add-on. Think of it as a dictionary of all the PowerShell commands available to you.
PowerShell is configured to not run any scripts other than those that are already part of Windows. You will need to change it so you can run your own scripts.
In the script window, copy and paste the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
The Set-ExecutionPolicy part of it is called a cmdlet (pronounced cmdlet). Cmdlets are powerful. Think of them as short commands that you can give Windows, and Windows will do a bunch of more complicated things to satisfy your command.
An analogy would be: advise your child to clean the room. Briefly and to the point. If your child is good at cleaning a room, they’ll go out and make the bed, put the laundry in the basket, put their toys and books on the shelves, and maybe even vacuum the floor. They understand what the Clean-YourRoom cmdlet means.
At the top of the window, you will see a green arrow. Click on it to run the command.
The -ExecutionPolicy part tells Set-ExecutionPolicy which policy to set. This is a parameter. He says, “I want you to work according to these principles and do just that. He tells him to use a specific RemoteSigned policy.
The RemoteSigned policy states that PowerShell cannot execute or run any script downloaded from the Internet unless signed by a trusted publisher.
In a roundabout way, it tells PowerShell that you can run any script you create locally because it doesn’t need to be signed by a remote trusted publisher. Now your PowerShell script can run whatever script you write on your computer.
Let’s take a look at a script for deleting temporary files. It’s not as powerful and thorough as CCleaner, but CCleaner has its own set of problems.
Use PowerShell to create hard drive space
We’ll walk through this scenario line by line so you can see how it works. The script will be shown in full at the end, so you can copy and paste it if you like.
$ TempFileLocation = “C: Users username Appdata Local Temp *”
Anything before the dollar sign is a variable name. Think of it like a wallet to put your valuables in. The wallet reminds us of the dollar sign, valuables sound like variables, so we’ll remember that too. We create a wallet or variable named $ TempFileLocation. The equal sign tells PowerShell what to insert into this variable.
In this case, we specify the location of the Windows temporary files – C: Users username AppData Local Temp *. We do this for two reasons; it’s where it’s always safe to delete files from, and we’re going to use it to tell PowerShell where it should go to delete files.
Where your username appears in that space, replace it with your username. This is the username that you use to log into your computer. The asterisk (*) at the end is a wildcard character. It represents everything in the Temp folder because everything in the Temp folder is temporary and we want to delete it.
Double quotes around the location are also important. This tells PowerShell what the string is inside. Think of a string as a string of letters and symbols. If these were regular numbers, we wouldn’t be using quotes.
$ TempFile = Get-ChildItem $ TempFileLocation -Recurse
We are making another variable. This time we’re going to use the command to come up with something to put in the $ TempFile variable.
Get-ChildItem
Another thing you’ll notice about cmdlets like Get-ChildItem is that they are completely readable. See how the first word is a verb? All cmdlets start with action words, so you know immediately what they are doing. ChildItem is two nouns.
The second part of the cmdlet always tells us which PowerShell action will take. ChildItem means all children of the parent location. It’s like saying, “Get all the files that are in a folder, with the files being children and the folder being the parent.
Which cmdlet gets the children? Everything in the $ TempFileLocation variable. PowerShell will jump to the location we put in $ TempFileLocation earlier and get all the children that are there. Then it will put them in the $ TempFile variable.
So what about the -Recurse part? This tells Get-ChildItem to go through everything in that location. Don’t immediately upload files to the parent folder. If there are folders in that folder, get all their children, their children, and so on. We’re going to get them all.
$ TempFileCount = ($ TempFile) .count
Yes, we are creating a third variable called $ TempFileCount, and we are going to put a number in this variable. Where is the number? Well ($ TempFile) .count will give us that number. You probably guessed that the .count part will count all the files we just saved in $ TempFile.
Why did we do it? Mainly because it’s nice to know how many junk files we clean up with the rest of the script to determine how efficient it is.
if ($ TempFileCount -eq 0)
We are now setting up the conditional expression. You can see that he is asking if. If anything? If the brackets are true or false. The parentheses are important, otherwise If doesn’t know what to compare. Here he asks if the number we stored in $ TempFileCount is zero.
-Eq is short for equality. This is a variation on the comparison operator. It’s like telling your kids, “If your room is clean, great, we’re going to do something ” This statement says that if the number stored in $ TempFileCount is zero, do the following.
{Write-Host “There are no files in the $ TempFileLocation folder” -ForegroundColor Green}
This will happen if $ TempFileCount is zero. The curly braces are important. They tell PowerShell to only do what’s inside them if $ TempFileCount is zero.
It will write to the host or screen: “There are no files in the C: Users username Appdata Local Temp * folder.” The -ForegroundColor parameter at the end tells PowerShell to turn the text green. It just makes it easier to distinguish from the error message, which is usually red.
Otherwise
You know what else it means. This is the “Better clean your room or else…†part to see if your child has cleaned his room. This is what happens if $ TempFileCount is nonzero.
{$ TempFile | Remove-Item -WhatIf -Confirm: $ false -Recurse -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue}
This indicates a jump to the current location stored in $ TempFile. The vertical line after it is called the pipe. It works like a real pipe because it tells PowerShell to pipe the contents of $ TempFile to the cmdlet as if the water were piping into the washing machine. The Remove-Item cmdlet then does what it says; it removes everything in that location.
The -WhatIf parameter is very important at this stage. It tells PowerShell to run the command, but just try it, don’t delete anything. Just show us what it would be like if we really did.
This allows you to test commands without changing anything on your computer. Leave -WhatIf there until you are sure the script will do what you want and nothing else. Then just remove that and the script will do its job.
The -Confirm: $ false parameter prevents the script from asking if you really want to delete the file. You know what -Recurse does. -Power means to remove that thing no matter what. -WarningAction is SilentlyContinue.
Thanks to this, the script will not warn you about what you are deleting. -ErrorAction is SilentlyContinue, so if there is any error in the process, it just keeps running.
Now we are on the last line.
Write-Host “Cleaned up the $ TempFileCount files in the $ TempFileLocation folder” -ForegroundColor Green}
Just like the first time, Write-Host will output the following sentence so that we know what happened. It will tell us how many files have been removed from the folder it just processed, and it will highlight it in green so that they are easy to spot.
Let’s look at the whole scenario:
$ TempFileLocation = “C: Users guymcd Appdata Local Temp *”
$ TempFile = Get-ChildItem $ TempFileLocation -Recurse
$ TempFileCount = ($ TempFile) .count
if ($ TempFileCount -eq “0”) {
Write-Host “There are no files in the $ TempFileLocation folder” – ForegroundColor Green
}
Otherwise {
$ TempFile | Remove-Item -Confirm: $ false -Recurse -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
Write-Host “Cleaned up $ TempFileCount files in $ TempFileLocation folder” -ForegroundColor Green
}
You can copy and paste it into your PowerShell ISE and save it as something like delete-TemporaryFiles.ps1. Now you can stick to the naming convention for cmdlets when you understand them.
If the code doesn’t work for you for some reason, you can also download the PowerShell script we created and just run it. Just unzip it to view the script file.
Whenever you want to run this script, just right-click it and choose Run with PowerShell. The PowerShell console will pop up for a second or two while your script does its job, then disappear if there are no errors.
Where can I learn more from PowerShell?
It seems like a lot! For your first PowerShell script, this is not bad. If you’ve come this far, applaud yourself. You have learned a lot today, but now you want to know more. Wonderful!
There are many resources on the Internet for more information on PowerShell. A good place to start is in our article “Creating a List of Programs to Run Using the Command Prompt or PowerShell”. Then check out other resources:
- Getting Started with Microsoft PowerShell – Microsoft Virtual Academy
- PowerShell Tutorial – TutorialsPoint.com
– –