by Barry Dysert
(last updated December 26, 2016)
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:
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.
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
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
One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...Discover More
FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."