Skip to content

Introduction to PowerShell: Learn to use Commands & Scripts

PowerShell

Command-line interfaces allow you to control your computer in a way that most people can’t. You can customize settings that you can’t access from the control panel, you can move files that you can’t normally even see, and you can run processes in the background that are normally boring and repetitive. Powershell is great for getting a degree of control over your computer that you don’t normally have.

Powershell vs. Other Command-Line Interfaces

If you’re a Mac or Linux user, you’ve probably used other command-line interfaces before. In fact, if you’re a Linux user you’ve Definitely used it. This is what’s known as a Unix command-line interface, and it’s not the same as Powershell, so most of the commands for a Unix system won’t work in Powershell. It’s a totally different system than the one that you’re used to. So let’s start learning the difference!

Getting Powershell Up and Running

If you have Windows 7 or beyond, Powershell comes installed with your system. If you have anything before Windows 7 (Vista and XP), then it is recommended to update your windows. Once you are able to find Powershell in your start menu, you’re ready to go!

To run Powershell, open the Start menu and type “Powershell” and double click the icon. Just like you’d open any other app! Once you’ve opened Powershell, here’s the first command that any skilled developer or programmer will be sure to use:

get-help

This opens up a “help” menu within Powershell that helps you figure out the commands that you need for different programs.

If you want to know commands that directly affect Windows services, type:

get-help services

If you want to also access the online Microsoft tech help desk, you can use this command:

get-help start-service -online

This will connect you to a fully updated database of services and commands that you can use to program your computer.

Basic Commands Example

The basic commands of Powershell are mostly the same as other command-line interfaces. Here’s a short list of the basic Powershell commands:

ls: list the contents of a directory

cd: change the directory (used like cd .\users)

del: type del followed by the file or directory (like del \users\programfiles\steam)

md: make a new directory. This one is different from the Unix “mkdir”

echo: display a line of text in the code, (as in echo “display this line”)

cls: clear the console of all text. Great for when it gets all cluttered to death.

One thing to note is the cmdlets (the commands we’ve just been talking about) use backslashes () as opposed to more traditional forward slashes. Keep this in mind so that you don’t get an error message for no apparent reason.

Common Syntax

Just like traditional languages such as English, Powershell uses syntax for speaking to the computer. The primary rule of syntax in Powershell is a verb-subject “sentence” structure. For example:

get-itemproperty c:\windows\regedit.exe

This retrieves information about the object (c:\windows\regedit.exe), but the first part is a verb (get-itemproperty).

This is the structure that all commands in the command-line interface will follow. First, you will type in the command, which will be a verb. Then you will make an “argument”, which is whatever you want to know about a given file or data set. In the above example, “get-itemproperty” is the command, and “c:\windows\regedit.exe” is the argument.

There are a finite number of commands that you can use, but the only limit to the number of arguments that you can use is the storage space on your computer.

Objects on the screen can be closer than they seem

When you retrieve a file like you just saw in the example, there’s a lot going on behind the scenes that you probably didn’t notice. Here are a few examples.

When you typed the command: “get-itemproperty c:\windows\regedit.exe”, you are showing much more than most command-line interfaces would normally allow. Most of the time a command-line interface will show you information, but the ways that you can interact with it are fairly limited.

Powershell has changed this way of dealing with the command line and now allows users to be a lot more advanced with manipulating information on the screen.

To get access to even more information about the file that you’re using, use the following command:

get-itemproperty c:\windows\regedit.exe | format-list

This command, which uses the pipe (|) allows the user to gain access to even more information about the file than a normal user could. But we can go even further than that: when you use this command, it sends all of the information in the file that you opened into a second file (format-list), and that way you don’t have to worry about typing the entire file location into the command line every time you want to bring up information about the file. You have a smaller variable for it now. If you’re at all familiar with Python, it’s similar to creating variables and assigning them values to use later.

If you want to get a lot of information about a collection of files, you could use a command like this:

dir | get-itemproperty | format-list

This command gets information about whatever directory you are using at the time, and pipes all of that information into the “format-list” variable and gives you a detailed list of information about all the files that you are using in that directory. It’s easy to use and doesn’t take much work to figure out a lot about the files that you’re using.

Scripts in the Powershell

If you’ve ever studied computer science before, you’ve probably heard of scripts before. If you haven’t, they’re pretty easy to grasp. Scripts work just like play scripts; they give the actors (i.e. the computer parts) the lines and instructions for what they’re supposed to do at certain times. Most of the time Windows executable scripts will have a .exe ending, but they could also take on a variety of forms, depending on the type of code.

The scripts that you will write and execute in Powershell are .ps1 scripts, and they are generally simpler than scripts like Java or Python. These scripts are used to execute simple, usually automated tasks for your computer to follow. The limits of what the script can do are limited by the abilities of Powershell itself. For example, the scripts are not going to be able to hack someone else’s computer. Don’t get too excited! But you can do things like automating downloads for specifics times, running your antivirus at custom times… Things like that!

A Script Example

In the first example that we used, the get-itemproperty command, we retrieved information about the file and piped that into different variables. Don’t worry if you can’t see the practical value of that now; we’ll need to do a little more work before it makes complete sense.

Here’s how to make your first script. First, what we’re going to do is open Notepad. Notepad is a text editor that doesn’t mess with formatting or fonts, unlike Word. You’re never going to want to write code in Word because you will get lots of formatting and spacing errors that you have to deal with later. Notepad takes away all of those issues with formatting and design.

We’re now going to do the same thing that we did with the get-itemproperty with a command script instead of typing the lines directly into the Powershell interface. Open Notepad, and type the following:

$list = dir
get-itemproperty $list | format-list | out-file out.txt

Here’s where you’re going to get tripped up if you’re used to Unix system command-line interfaces: you need to type, as in physically type, the $ sign. That is what creates the variable in a script. In Unix systems, the $ appears automatically at the beginning of every line, signifying that it’s now okay to type on that line.

Now all you have to do is save the Notepad as something simple, like the script.p1. Now, go back to the Powershell interface. What you’re going to do now is run the script by typing in the following:

.\script.ps1

And it should bring up the same information as the first series of commands did!

Why Do It With Scripts?

Why write with scripts? Wouldn’t it be a lot easier just to type in what you need to type instead of just writing a whole separate file to make the computer do something? Well, the fact that it’s a whole other file is actually what makes it so nice! Think about this: this script may not be very long, but often professional scripts that do very complicated computing operations can really be a pain to write. So if you had two computers or a series of office computers and you wanted to run the same operation on all of them, all you have to do is email the script to the computer that you want to run it on and then execute the script from Powershell. That saves you a lot of writing time!

One More Potential Problem

You may run into a problem here. That problem is that often, Windows blocks scripts that they themselves did not write. This is a security feature for two reasons: first, viruses normally run through scripts, and unidentified scripts are often malware. Second, even users that write their own scripts can damage their computers. For example, you could be writing a process that involves restarting your computer at certain times, but you could accidentally make your computer overclock instead, heating up the whole system. You just have to be careful and make sure that you’re not messing with too many variables and features, and make sure that the scripts you write don’t deal with anything too critical.

Here’s how you allow outside scripts: In Powershell, type:

set-executionpolicy remotesigned

This will make sure that you can run scripts that you wrote, but it still won’t allow scripts that come from the internet that Microsoft didn’t write. That way, you’re still secure and protected!

Making Your Scripts More Advanced

You can make your scripts do a whole lot more by adding more commands to the script that you wrote, script.ps1. Go back to that Notepad file and open it back up. Let’s replace that text with the following:

$list = dir
foreach ($item in $list) {
    $fn = $item.name “_.txt”
    get-itemproperty $item | format-list | out-file $fn
}

This will create a series of text files for the items in the directory that the script is looking at. Note that this script even has a way of figuring out what to name each file! The $fn tells the program to name the file whatever the filename is ($item.name) and add a .txt to the end of it.

You can also see the “foreach” command. This is a useful command that has the script look at each item in the directory and then run a command for each one. Each item will be processed according to whatever’s in the { } brackets. Then it does the same thing to each file that we did to the directory before: it gets the item name, makes a list, and outputs a text file with information about each individual file. Using a script saves you a lot of typing; it would be a bit more complicated to run that from Powershell, and if you accidentally backspaced it your progress wouldn’t be saved. Now all you have to do is type in .\script.ps1 to run it!

You’ll notice now that there are lots of text files clogging up the screen now; depending on how many files are in the directory. It really makes the screen seem a bit cramped… Let’s get rid of a bunch of the text files at the same time with another script! Write this script in Powershell:

$list = dir
foreach ($item in $list) {
    $fn = $item.name “_.txt”
    if (test-path $fn) {
        del $fn
    }
}

Recap and Review

So here’s what we’ve learned so far. Scripts are longer, text-based ways to enter a lot of commands at the same time into Powershell. They’re great for when you want to make the computer follow a series of commands instead of just doing one thing at a time. And that last sentence is probably the best description of what a script does: it’s like running a command in Powershell, but running several commands at the same time instead of just one.

Notepad is a useful coder’s tool that allows you to create your script and save it, as Powershell does not reliably save lines of code. That’s just not what it’s meant to do. That way, you can write the script in Notepad, save it as a .ps1 file, and execute it in Powershell with the command .\yourscript.ps1.

You can also send that executable file to other computers to run the same command on a different machine; you can execute it the same way! (From Powershell). And that’s all for scripts!

nv-author-image

Era Innovator

Era Innovator is a growing Technical Information Provider and a Web and App development company in India that offers clients ceaseless experience. Here you can find all the latest Tech related content which will help you in your daily needs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.