home > notes > powershell > Basics [ printer-friendly version ]
Basics
Profile
Simple GUI
ShowUI

PowerShell Basics

GOAL: Locate PowerShell on your Windows PC, create a desktop icon for it, run it, set your PowerShell script execution policy to allow you to run scripts, and write and run a simple PowerShell script.

Locate PowerShell

If your PC is running Windows 7 like mine, you've already got PowerShell installed as part of the OS. It's not advertised, but it's there. Click 'Start', then type powershell into the search box. The top item displayed should be 'Windows PowerShell'.

Create a desktop icon for PowerShell

While you've got the results on the screen from your search for powershell, right-click the 'Windows PowerShell' item, then click 'Send to', then click 'Desktop (create shortcut)'. This will create a desktop icon that will launch a PowerShell window. You could also pin PowerShell to the task bar, start menu, etc. - it's just a program like any other program on your machine.

Start PowerShell

Now that you have a handy way to launch a PowerShell window, double-click the PowerShell shortcut on your desktop and fire it up! The window that appears looks a bit like a DOS command prompt window. It's a command-line interface, and you should be familiar with the command line if you've done any scripting. Type 'exit' to close the window when you're finished.

Check and Set Script Execution Policy

By default, your PowerShell environment is restricted. This means that you cannot execute PowerShell scripts. I assume this is set this way to allow Windows internal functions to use scripts to manage the system while protecting you from yourself. In any case, it's easy to adjust the script execution policy to allow you to wtite and execute scripts.

First, you can check the current script execution policy from the PowerShell command line:

get-executionpolicy
    

On a default Windows 7 machine, this command will return a value of 'Restricted'.

There are a number of available choices when it comes to setting script execution policy, and I'm not going to cover all of that here. For our initial tinkering, the following command provides a reasonable level of security that lets us write and run scripts and is limited in scope:

set-executionpolicy RemoteSigned -scope currentuser
    

This change is persisted in the Windows registry, and it will remain in effect for future PowerShell sessions. You can verify that the change has been applied by running get-executionpolicy again and exiting and restarting PowerShell and verifying that the change is persistent.

Again, note that scripting functionality is disabled by default. You must change the policy to allow yourself to run scripts if you expect to get anything out of the rest of this tutorial.

Write a simple script

Now that you've set your execution policy, you're ready to write your first script. To keep things tidy, you'll want to create a directory that will contain your PowerShell scripts. Create a directory named MyScripts in your Windows user directory. If your name is fred, the new directory will be fred/MyScripts. When you launch the PowerShell window, you'll be in the fred directory and you can cd MyScripts to get into your scripts directory.

PowerShell scripts have the file extension .ps1, and you can edit them in Notepad. Here's an interesting way to create a script file and edit it with Notepad:

cd MyScripts
New-Item -Path derp.ps1 -ItemType file -Force
notepad derp.ps1
    

There's nothing magic about creating your script file with New-Item - you could just as easily fire up Notepad in the usual way and save your script as derp.ps1 in the MyScripts directory.

Here's a simple "Hello World" script:

write-host "Hello, world!"
$greeting = "Hola"
$name = "amigo"
$salutation = "$greeting" + ", " + "$name" + "!"
write-host "$salutation"


$IPADDR = ((Test-Connection $env:computername -Count 1).ipv4address).ipaddresstostring
write-host "Wow! Your IP address is $IPADDR"
    
This script shows you a few very basic things about how you can write strings, along with a tiny glimpse of how PowerShell can see and interact with your OS. Type this script into derp.ps1 and save it into your MyScripts directory.

Run a simple script

Running the script is simple, but you have to explicitly provide the path to it. [Unix users: your current working directory is not in $PATH.] Assuming you're in your MyScripts directory, you can run the script like this:

.\derp.ps1
    
Here's what you should see:
Hello, world!
Hola, amigo!
Wow! Your IP address is ###.###.###.###
    
Amazing, right? Hey - what did you expect from your first PowerShell script? Take a bow - you are now a PowerShell scripter.

Summary

By working through these steps, you've learned a number of things:

  • Your Windows machine provides a scripting environment that is surprisingly powerful, especially when compared to the limited .BAT scripting capability of the old days of DOS.
  • You can set your security policy to enable you to write your own scripts.
  • The PowerShell scripting environment has deep visibility into the internals of your OS and machine setup, enabling you to write complex scripts that are interesting and useful.

home Meddle ye not in the affairs of dragons, for thou art crunchy and good with ketchup! privacy