Introduction to PowerShell

by Barry Dysert
(last updated December 26, 2016)

5

PowerShell is built-in Windows utility that includes an extensive scripting language. If you're used to writing .bat files to perform command-line actions, you will be impressed with all that PowerShell can do. Please note that I am not (yet) a PowerShell guru. As I've been working with it, though, it has come to impress me so much that I wanted to introduce the loyal readers of WindowsTips to it.

You can start PowerShell in a few different ways, and it depends on what version of Windows you're running as to what you'll see. Therefore, although I'll mention different ways of invoking it, I'll concentrate on using PowerShell from the command line.

One way to invoke PowerShell is to open a Command Line window (which is covered under another tip), and type "PowerShell" (without the quote marks) followed by Enter. Another way is to use the search capabilities of Windows to search for "Windows PowerShell." A third way to again search for "Windows PowerShell" and choose the search result called "Windows PowerShell ISE." When you select this item, you'll see a split screen where you can interactively enter PowerShell commands in the bottom of the screen and also build a PowerShell script file in the top of the screen. This split screen looks like this: (See Figure 1.)

Figure 1. PowerShell ISE.

Once you're in PowerShell, you deal mostly with "cmdlets". The cmdlets follow a pattern of verb-noun. For example, to get help within PowerShell, type Get-Help and press Enter. There will be other tips about PowerShell, but if you can't wait, it is suggested that you start by going through the following help topics:

PS> Get-Help Get-Command
PS> Get-Help Get-Member
PS> Get-Help Where-Object
PS> Get-Help About_Object

The first command returns information about cmdlets, while the second gets the properties and methods of an object. The third command filters object properties and the final command explains the use of objects in PowerShell.

A PowerShell script can be a bit on the verbose side, so it incorporates things called aliases, which are shortcuts for many of the more popular cmdlets. For example, instead of typing Get-Help, you can just type Help. To see a list of existing aliases, type Get-Alias at the PS prompt.

PowerShell scripts are normal text files that end with a .ps1 extension and contain a sequence of cmdlets designed to perform a function. For example, you could write a PowerShell script that displays "Hello" to the console. Before you can run scripts, though, you must first establish the security environment. Follow these steps:

Invoke the Windows Command Line, specifying Run As Administrator

Invoke PowerShell by typing "PowerShell" at the command prompt

At the PowerShell prompt type Set-ExecutionPolicy RemoteSigned

These steps only need to be done once for you to be able to henceforth run scripts. Now launch Notepad and type into it the following:

Write-Output "Hello"

Exit Notepad, naming your file Hello.ps1, and be sure it's saved to the directory indicated by your PowerShell session. (The directory name is part of the PowerShell prompt; I have a C:\PowerShell directory where I store all my .ps1 files.) From your PowerShell prompt type ".\Hello.ps1" and you should see your script executed, i.e., the word "Hello" will be displayed on the screen.

There is plenty more that can be said about PowerShell, but since this is an introductory tip, I'll just mention a few more things. Since it's always good practice to comment your scripts you should know that the PowerShell comment character is the "#" symbol. Anything that appears to the right of this character is ignored. And, speaking of special characters, if you need to continue a PowerShell line to another line, end the first line with an accent grave ("`"), sometimes called a backtick character or a backwards apostrophe.

Another thing to get used to is the ability to "pipe" the output of one command to another. PowerShell scripts often have their cmdlets pipe to other cmdlets. The pipe character is the "|" symbol. So, for example, you can format the output of one cmdlet different ways by piping into one of the "format" cmdlets. Let's say you wanted to get the properties of a file named "C:\Temp\VmwareStatus.log". You can do this using the Get-ItemProperty cmdlet like so:

PS> Get-ItemProperty C:\Temp\VmwareStatus.log

By default, the file's properties will be displayed in a table format. If you want the properties displayed in a list, you can pipe the output to the Format-List cmdlet:

PS> Get-ItemProperty C:\Temp\VmwareStatus.log | Format-List

Finally, if you want to return to the familiar command-line environment, just type "cmd" at the PowerShell prompt. To exit this environment, just type "Exit". To exit PowerShell, you also type "Exit".

 This tip (13506) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Creating a Disk Partition

Partitioning your disk offers a quick and efficient means to make good use of your disk resources. By creating disk ...

Discover More

Breaking a Network Drive Mapping Association

Windows makes it easy to break the association between a network drive and its assigned letter. This tip explains how.

Discover More

Making a File Read-Only

If you have an important file that you want to be sure doesn't get accidentally edited, you can set it to read-only. This tip ...

Discover More
More WindowsTips

Using Batch Files, Part 4: The CALL Statement

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More

Using the FOR Statement

In another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left as ...

Discover More

Using Batch Files, Part 1

This tip is part of a series that shows you how to create and use Windows batch files. It introduces a few commands and the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two minus 2?

2017-01-19 16:02:01

Ray Ross

Thanks for this tip. I look forward to more Power Shell tips.


2016-12-28 16:35:15

Ted Duke

Thanks. The text has some missing and required quote marks as in type:

Set-ExecutionPolicy RemoteSigned

which worked Ok after I typed

"Set-ExecutionPolicy RemoteSigned"


2016-12-28 15:37:02

Steve Harrison

To Barry.
Thanks for replying, I will persevere.


2016-12-27 17:14:43

Barry

There is no one place that I know of. Your best bet is probably to use the Help utility and do a lot of searching of the Web. Good luck.


2016-12-27 15:36:20

Steve Harrison

Thank you for a very good article. I, also, am trying to learn Powershell but am frustrated by a lack of very basic instructional information. Does anyone have any knowledge of a site providing such info. There are plenty of sites that assume a much greater starting knowledge than I have.
Cheers


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.