Introduction to PowerShell

Written by Barry Dysert (last updated November 30, 2020)

PowerShell is a 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 is to again search for "Windows PowerShell" and choose the search result called "Windows PowerShell ISE." In the latest version of Windows 10, when you select this item, you'll see a split screen where you can interactively enter PowerShell commands in the left side of the screen and also build a PowerShell script file in the left side of the screen. The Windows PowerShell ISE screen looks like this: (See Figure 1.)

Figure 1. Windows PowerShell ISE.

If you are using an earlier version of Windows 10, 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 may look like this: (See Figure 2.)

Figure 2. 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 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

Running Older Programs in Windows

You may still need to use programs that worked under older versions of Windows that don't work so well under Windows 7 or ...

Discover More

Understanding Process Monitor

A very useful tool in diagnosing what is going on with processes and/or files is the Process Monitor tool from ...

Discover More

Running a Batch File at a Scheduled Time

Once you become comfortable with batch files, chances are that you'll want to use them to perform various system ...

Discover More
More WindowsTips

Passing Parameters to a PowerShell Script

Like the older batch-file processor, PowerShell can accept parameters. This allows for flexibility in your script. This ...

Discover More

Counting the Number of Files or Subfolders Using PowerShell

Do you need to determine the number of files or subfolders there are in a folder? You can use PowerShell to quickly count ...

Discover More

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into ...

Discover More
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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips