Who doesn’t wish they could be a wizard and utter a few words to have magical things happen? Well, we’re not able to show you how to be a wizard, but we can show you how to do a little computer magic.

If you’ve got a Windows computer with Windows 7 on it, you’ve got PowerShell. Also, if you have a computer with Windows 7 on it, please update it for security’s sake.

Table of Contents
    Using PowerShell for Home Users – A Beginner’s Guide image 1

    But this article isn’t just for Windows people. The latest version of PowerShell is free and can be installed and used on Mac’s and Linux computers.

    That’s important because what you learn about PowerShell can be used on almost any computer now. Who knows? You might take this skill to the next level and venture into a career in Information Technology.

    What is PowerShell?

    The high-tech answer from Microsoft is that it is a, “…command-line shell designed especially for system administrators.” Sounds intimidating. But it’s not. Do you take care of your computer? Yes, then you’re the system administrator in your house.

    Do you tell your computer what to do with clicks and keystrokes? Yes, so think of the command-line shell as just another window that you type things in to tell your computer what to do. You can do this.

    PowerShell is like a programming language, but not as cryptic as most. It really reads a lot like regular English, which was something Microsoft strove for so that it could be picked up by non-programmers.

    It’s a way of writing a few lines of commands, called a script, to make the Windows operating system do something that you want it to do. Then, you can save those lines in a file and run it with a click or schedule it to run periodically.

    What Can You Do with PowerShell?

    The high-level point of PowerShell is to be able to automate tasks, so you don’t have to waste your time doing mundane things repeatedly. For a professional Systems Administrator, that could be something like creating new users, generating passwords for them, and send an e-mail with the details to their new supervisor.

    Manually done, with clicks and typing, that process can take 5 minutes to as much as an hour or more. With the right scripts, the Systems Administrator may not even have to do any part of that.

    But you want to know what you can do with PowerShell at home. Pretty much anything that you don’t like doing again and again. Use it to free up space on your hard drive by deleting temporary and log files you don’t need.

    Put a curfew on your kid’s computer. Rename or organize a bunch of files. That’s the beauty of PowerShell. Almost anything you can do on your computer, you can create a PowerShell script to automate and run with a click or on a schedule.

    How Do I Use PowerShell?

    The easiest way to work with PowerShell is in the PowerShell Integrated Scripting Environment (ISE). You can find it by clicking on Start and typing powershell ise into the search bar in Windows 10. You should see it as shown below.

    Using PowerShell for Home Users – A Beginner’s Guide image 2

    For the first time that we use it, we’re going to run it as the Administrator. To do this, you must first have administrator rights on your computer. In your Start Menu, right-click on PowerShell ISE, then click on Run as Administrator.

    Using PowerShell for Home Users – A Beginner’s Guide image 3

    You may get a User Access Control (UAC) warning pop-up asking if you’re sure you want to do this. Click Yes.

    Using PowerShell for Home Users – A Beginner’s Guide image 4

    Now you’re looking at the PowerShell IDE. The top pane of the window (1) is the scripting pane. This is where you will write your script. The bottom pane of the window (2) is the console area. When you test your script, you’ll see the output in this pane.

    This is also where you’ll see error messages and such that will help you fix and make your scripts better. The pane on the right-side of the window (3) is the command add-on. Think of it as a dictionary of all the PowerShell commands available to you.

    Using PowerShell for Home Users – A Beginner’s Guide image 5

    PowerShell comes set to not run any script other than those already a part of Windows. You’ll need to change it so that you can run your own scripts.

    In the scripting window, copy and paste the following:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

    The Set-ExecutionPolicy part of it is called a cmdlet (pronounced commandlet). Cmdlets are powerful things. Think of them as short commands that you can give Windows, and Windows will go do a bunch of more complicated stuff to satisfy your command.

    An analogy would be telling your kid to clean their room. Short and to the point. If your kid is well-versed in room cleaning, then they will go and make their bed, put their laundry in the basket, shelve their toys and books, and maybe even vacuum the floor. They understand that the cmdlet Clean-YourRoom meant all that.

    At the top of the window, you’ll see a green arrowhead. Click on that to run the command.

    Using PowerShell for Home Users – A Beginner’s Guide image 6

    The -ExecutionPolicy part is telling Set-ExecutionPolicy what policy to set. It’s a parameter. It’s saying, “I want you to work within these guidelines and do specifically this. It tells it to use the specific policy of RemoteSigned.

    The RemoteSigned policy states that PowerShell may not execute, or run, any script that was downloaded from the Internet unless it was signed by a trusted publisher.

    In a roundabout way, it tells PowerShell that it’s fine to run any script created locally, because those don’t need to be signed by a remote trusted publisher. Now, your PowerShell script will be able to run any script you write on your computer.

    Let’s look at a script to delete temporary files. It isn’t as powerful or thorough as CCleaner, but CCleaner comes with its own set of problems.

    Use PowerShell to Create Space on Your Hard Drive

    We’ll break down this script, line by line, so you can see how it works. At the end, the script will be shown completely so you can copy and paste it if you’d like.

    $TempFileLocation = "C:\Users\username\Appdata\Local\Temp\*"

    Anything with a dollar sign in front of it is a variable name. Think of it like a wallet to put valuable stuff in. Wallet reminds us of the dollar sign, valuables sounds like variable, so we’ll remember that too. We’re creating a wallet, or variable, named $TempFileLocation. The equal sign tells PowerShell what to put in that variable.

    In this case, we’re putting in the location of a Windows’ temporary files location – C:\Users\username\AppData\Local\Temp\*. We’re doing this for two reasons; it’s a location that is always safe to delete files from, and we’re going to use it to tell PowerShell where it must go to delete files.

    Where it says username in that location, swap it for your user name. That’s the user name that you use to log in to your computer. The asterisk (*) at the end of it is a wild card. What it represents is everything in the folder Temp, because everything in the Temp folder is temporary and we want to delete it.

    The double-quotes around the location is important too. That tells PowerShell that what’s in there is a string. Think of string as a string of letters and characters. If it were ordinary numbers, we wouldn’t use the quotes.

    $TempFile = Get-ChildItem $TempFileLocation -Recurse

    We’re making another variable. This time we’re going to use a 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 it is totally readable. See how the first word is a verb? Cmdlets all begin with action words, so you immediately know what it’s doing. ChildItem is two nouns.

    The second part of the cmdlet is always going to tell us to what PowerShell will apply the action. ChildItem means all the children of a parent location. This is like saying get all the files that are in a folder, with files being the children and the folder being the parent.

    What is the cmdlet getting the child items of? Everything in the variable $TempFileLocation. PowerShell is going to go to the location that we put in $TempFileLocation earlier and get all the child items that are there. Then, it will put them into the variable $TempFile.

    So, what’s with the -Recurse part? That’s telling Get-ChildItem to go through everything in that location. Don’t just go get the files immediately inside the parent folder. If there are folders in that folder, get all their children too, and their children and so on. We’re going to get them all.

    $TempFileCount = ($TempFile).count

    Yes, we’re creating a third variable called $TempFileCount, and we’re going to put a number in that variable. Where’s the number? Well, ($TempFile).count is going to get us that number. You’ve probably figured out that the .count part is going to do the counting of all the files that we just stored in $TempFile.

    Why did we do this? Mostly because it’s nice to know how many useless files we’re cleaning out with the rest of the script so we can tell how effective it was.

    if($TempFileCount -eq 0)

    Now we’re setting up a conditional statement. You can see that it’s asking ‘if’. If what? If the thing in the brackets is true or false. The brackets are important, otherwise the If doesn’t know what to compare. Here, it’s asking if the number we stored in $TempFileCount is equal to zero.

    The -eq is the shorthand for equals. It’s a type of comparison operator. It’s like telling your kids, “If your room is clean, great, we’re going to do something…” This statement is saying if the number that was stored in $TempFileCount is -equal to zero do the next thing.

    {Write-Host "There are no files in the folder $TempFileLocation" -ForegroundColor Green}

    That’s the thing that will happen if $TempFileCount is zero. The curly brackets are important. They tell PowerShell to do only what is inside them if $TempFileCount equals zero.

    It will write to the host, or the screen, “There are no files in the folder C:\Users\username\Appdata\Local\Temp\*”. The parameter at the end, –ForegroundColor tells PowerShell to make the text green. That just makes it easier to distinguish from an error message which is normally red.

    Else

    You know what else means. This is the, “Your room better be clean or else…” part of checking if your kid cleaned their room. It’s what will happen if $TempFileCount is not zero.

    {$TempFile | Remove-Item -WhatIf -Confirm:$false -Recurse -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue}

    This says go to the current location stored in $TempFile. The vertical line after it is called a pipe. It does function kind of like a real-life pipe as it tells PowerShell to funnel the contents of $TempFile into the cmdlet, as though it was water being piped into a washing machine. Then the cmdlet Remove-Item does what it says; it removes whatever is at that location.

    The -WhatIf parameter is very important at this stage. It tells PowerShell to run the command, but only try it out, don’t remove anything. Just show us what would happen if we really did this.

    This allows you to test commands without changing anything on your computer. Leave the -WhatIf in there until you’re comfortable that the script is going to do what you want it to do and nothing else. Then just delete that out and the script will do its job.

    Using PowerShell for Home Users – A Beginner’s Guide image 7

    The parameter -Confirm:$false stops the script from asking if you really want to delete the file. You know what -Recurse does. -Force means delete that thing no matter what. -WarningAction is set to SilentlyContinue.

    This prevents the script from giving you warnings about what you’re deleting. -ErrorAction is set to SilentlyContinue so that if there’s any sort of error in the process, it just keeps chugging along.

    Now we’re at the last line.

    Write-Host "Cleared $TempFileCount files in the folder $TempFileLocation" -ForegroundColor Green}

    Just like Write-Host did the first time, it’s going to output the following sentence, so we know what has happened. It will tell us how many files were deleted from the folder it just processed and do it in green to make it easy to spot.

    Let’s look at the script altogether:

    $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 folder $TempFileLocation" - ForegroundColor Green
    }
    Else {
    $TempFile | Remove-Item -Confirm:$false -Recurse -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
    Write-Host "Cleared $TempFileCount files in the folder $TempFileLocation" -ForegroundColor Green
    }

    You can copy and paste this into your PowerShell ISE and save it as something like delete-TemporaryFiles.ps1. You may as well stick with the naming convention of cmdlets now that you understand them.

    If the code is not working for you for some reason, you can also download the PowerShell script we created and just run it. Just unzip it first to view the script file.

    Whenever you want to run this script, just right-click on it and choose Run with PowerShell. A PowerShell console will pop up for a second or two, while your script does its thing, and then will disappear if there are no errors.

    Using PowerShell for Home Users – A Beginner’s Guide image 8

    Where Can I Learn More PowerShell?

    That seems like a lot! For your first PowerShell script it is a fair bit. If you’ve made it this far, applaud yourself. You’ve learned a lot today, but now you want to learn more. That’s great!

    There are a lot of resources on the Internet for learning more about PowerShell. A good place to start is our article, “Generate a List of Startup Programs via Command Line or PowerShell”. Then check out these other resources:

    Leave a Reply

    Your email address will not be published. Required fields are marked *