Manage Learn to apply best practices and optimize your operations.

Simple PowerShell tips and tricks every Windows admin should know

No GUI? No problem. Our expert shares his best PowerShell tips to help IT administrators become proficient in this powerful scripting language.

PowerShell is perhaps the biggest advancement in Windows management since Microsoft Systems Management Server....

From a single crunch command-line console, you can perform hundreds, if not thousands, of management and administrative operations across vast swaths of servers. It is the preferred management method in Windows Server 2012 and later releases, and most of the Microsoft server product ecosystem is built using a "PowerShell-first, GUI-next" mentality.

PowerShell is powerful and relatively accessible, but don't be fooled. It is a new language and will take time for admins to become proficient in it. We must start somewhere, so here are some basic PowerShell tips and tricks.

It's practically impossible to truly be proficient in PowerShell without knowing how to find what management actions you can perform. One of the more useful PowerShell tips you'll use involves the Get-Command cmdlet, which lists all of the commands in your current PowerShell session (Figure 1).

Current PowerShell session commands
Figure 1: The Get-Command cmdlet lists all of the commands in the current PowerShell session.

You would want to refine the cmdlet list for daily use, which is what the –Name switch can do. It will search for any cmdlets that match the syntax you select. For example, if I wanted to use PowerShell for something to do with volumes, I could use commands such as Initialize-Volume, Format-Volume or Get-BitLockerVolume, depending on what I want to do (Figure 2):

Results from PowerShell search on volumes
Figure 2: These are the commands that would appear if admins searched what PowerShell commands to use for an action involving volumes.

If you have specific questions from there, use get-help in front of the command to learn more about that specific task.

Using the PowerShell pipe and "this thing" cmdlet

One of the most useful aspects of PowerShell is its ability to get information about an object and then funnel the search results into another cmdlet that performs some action on that object.

This transmission of a result set from one cmdlet to another is called "piping the output." You simply use the pipe character -- "|" -- above the backslash on your keyboard to accomplish this. We saw in the above example where I used the Get-Command cmdlet to get a list of commands. This was followed by piping the cmdlet output into the Measure-Output cmdlet, which gave me the count of the number of objects that were available.

But the pipe feature can do a lot more. We'll want to perform an action on a result subset most of the time. For example, we could get a list of running processes and then only display the ones that are Microsoft Excel. For that, we'd make use of the pipe and a nice cmdlet called Where-Object, which lets us do something for every object where certain conditions exist.

First a little shortcut: You will encounter the $_ characters a lot in PowerShell scripting and development. There is a rather technical name for it which is not terribly intuitive, so I call it the "this thing" notation because you are asking PowerShell to do something regarding "this thing."

To use our previous example, you would use the Get-Process cmdlet, which in and of itself makes a character-based version of Windows Task Manager. Then you would pipe the output -- which lists processes and information like how much CPU time a process is taking up, how much memory -- to the Where-Object cmdlet, which acts like a filter. Then use curly brackets to define the criteria for the conditions. Next, use the "this thing" notation, a period, to let PowerShell know we want some information about this thing, and the name property to find out the name of the process (Figure 3).

Use this notation for a specific action
Figure 3: This example shows what admins would see when trying to use the "this thing" notation for a specific action.

The –eq is the criteria for equals. There are other criteria you can use as well, and you will get an idea of how those work by trying different things in PowerShell.

How to trigger and save scripts

Another of my PowerShell tips involves knowing how to trigger scripts. Scripts are really nothing but text files saved to a given place in the file system, but they must have a .ps1 file extension. In some cases, you may also need to unblock the script from being run, particularly if you begin running scripts generated on other systems or downloaded from the Internet.

If you have commands you like for performing a certain task, save them as a script by writing them into Notepad and then saving the document as a .ps1 file. Once done, you may want to right-click the file you just saved in Windows Explorer and click the Unblock button on that property page; this tells Windows you trust this script and want it to run without prompting you, but this step is not always required.

If you don't already know PowerShell, start playing with it. It's the next big thing for Windows administration. Your future career development could depend on it, so make use of these PowerShell tips.

About the author:
Jonathan Hassell is an author, consultant and speaker on a variety of IT topics. His published works include RADIUSHardening WindowsUsing Microsoft Windows Small Business Server 2003 and Learning Windows Server 2003. Jonathan also speaks worldwide on topics ranging from networking and security to Windows administration. He is president of 82 Ventures LLC, based in North Carolina, and is currently an editor for Apress Media LLC, a publishing company that specializes in books for programmers and IT professionals. 

Dig Deeper on Windows administration tools