Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

These 5 Windows PowerShell commands are so good, they feel like cheating

PowerShell logo over a script
Jorge Aguilar / How To Geek | PowerShell

If you work in PowerShell regularly, you've probably retyped commands you forgot, manually checked log files, or logged into servers one by one. That ends up being annoying in the long run, and it's frustrating knowing you have to do it repeatedly. Some cmdlets can fix those problems for you, and end up being very useful for everyday users .

Sending results to a searchable window

It really does mean quick access

Get-Service and Out-GridView in Windows PowerShell

Jorge Aguilar / How To Geek

When you're looking at a lot of text scrolling in your console, you're not going to be able to find specific information easily.Out-GridViewcmdlet help here. If you pipe your results into this cmdlet, it changes the text output into a separate window where your data is in a table. Instead of adjusting complex filtering code every time you want to find something, this command gives you point-and-click control.

The window has a search filter box at the top that lets you comb through thousands of lines of data, like dense event logs or background processes, just by typing a keyword. You can sort information by clicking column headers or adding criteria filters using drop-down menus to narrow down results without adding any more code.

Advertisement
Advertisement

Make sure to add-PassThrubecause it makes the grid window an interactive selection tool. It pauses your script and lets you highlight specific rows. Once you click OK, the window closes, and those items are sent back down the pipeline, so your script can keep working on them. This is a huge step to learning how to automate Windows .

Running code on many computers at once

Do more work in less time

Get-Service Select-Object and Invoke-Command in Windows PowerShell

Jorge Aguilar / How To Geek

If you run a lot of servers, you know what it is like to log into each machine one by one for checks or updates.Invoke-Commandsaves time because it runs code on hundreds of remote machines at the same time instead of visiting each server one by one. You likely don't need it in hundreds, but if you have four or five, this saves you a lot of time.

This command uses remoting protocols likeWinRMon Windows orSSHfor cross-platform setups to support one-to-many remoting. It sends instructions to every computer on your list and gathers the results on your console. When you pass a script block to the cmdlet, it sends your code to all target systems at the same time. As the remote runs finish, the results come back to your local session as objects, so you don't need to monitor each machine.

To help you track the incoming data, PowerShell adds aPSComputerNameproperty to each object to show which server it came from. This gives you a way to group and sort the data locally to find differences across your setup.

Finding past work using shortcuts

Stop retyping the same commands

#get in Windows PowerShell

Jorge Aguilar / How To Geek

If you spent time building a complex PowerShell command, you might realize a few days later that you need the same logic but can't remember the syntax. PowerShell has a command history system that is like a record of your past work and lets you retrieve commands you executed earlier.

Advertisement
Advertisement

When you enter a command, PowerShell saves it. Modern environments use built-in session history and persistent history that thePSReadLinemodule manages. While the built-in history only tracks commands in your current session and PowerShell deletes them when you close the window,PSReadLinekeeps a continuous record.

This module writes every command you type across all sessions into a file on your machine, so you don't lose your work across reboots. Instead of retyping long scripts, you can use a keyboard shortcut to pull up commands you used earlier. When you type the pound character#followed by a keyword from the command and press the Tab key, PowerShell searches your history for the most recent match. Just keep pressing the Tab key until you find what you need. I start with#getand then press tab, and it usually finds what I need faster.

Watching files update as it happens

See changes as they arrive

Powershell command with Get-Content in it

Jorge Aguilar / How To Geek

It's annoying to run the same command repeatedly just to see if a log file has been updated. If you want to see a file stream live as the system writes to it, theGet-Contentcmdlet with the-Waitparameter is a great way to watch it. Normally,Get-Contentreads a file and shows you its contents. However, the cmdlet acts differently when you add the-Waitparameter.

Instead of closing the file, PowerShell keeps the connection open and checks for new content every second. When the system adds new lines, you see them on your screen. Since you don't have to restart the command, it's a great way to watch for errors or track how a background process performs.

Advertisement
Advertisement

You can adjust this by combining-Waitwith code like-Tail. For example, if you're watching a very large log file, use-Tailto show just the last few lines before it starts waiting. This keeps your console from showing too much old data. If you use-TotalCountto ask for more lines than the file contains,Get-Contentshows its current content and waits until the system writes the other lines to the disk. This helps scripts that need to pause until a specific log event happens.

Narrowing down lists to find specific items

Find the needle in the haystack

All of the Get-Service and Out-GridView showing everthing running and stopped

Finding the specific information you need in a large dataset is hard, but filtering is a great solution. When you deal with too many results, theWhere-Objectcommand shortens lists to show just the important items. This takes the output from other commands and limits that data based on the rules you set. You can look for files that are too big or filter system services to find ones that haven't started.

TheWhere-Objectcmdlet is like a gatekeeper in the pipeline, checking every object and letting only the ones that match your criteria pass to the next command. PowerShell lets you set these rules using a simpler syntax that reads like natural language. This makes it easy to do quick matches, like checking if a service status equals a particular state.

For something more complex, you can use the scriptblock syntax with curly braces and variables to check multiple conditions at once. This lets you combine operators like-and-orto create specific filters.


Keep these commands handy for the future

None of these cmdlets need extra setup or third-party tools. They're already in PowerShell, and the only thing separating you from using them is knowing they exist. Once you start working them into your routine, you'll notice pretty quickly how much time you were wasting doing things the long way. Just make sure you write them down or bookmark this page so you can go back when you need them.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: