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


If you work regularly in PowerShell, you’ve probably retyped forgotten commands, manually checked log files, or accessed servers one by one. It gets annoying in the long run and it’s frustrating knowing you have to do it over and over again. Some cmdlets can solve these problems for you and be done very useful for everyday users.

Sending results to a searchable window

This means really fast access

Get-Service and Out-GridView in Windows PowerShell Credit: Jorge Aguilar / How To Geek

When you’re looking at a lot of text scrolling through your console, you won’t be able to find specific information easily. Out-GridView This is where the cmdlet helps. If you paste your results into this cmdlet, it changes the text output to a separate window with your data in a table. Instead of adjusting complex filtering code every time you want to find something, this command gives you point-and-click control.

At the top of the window is a search filter box that lets you scan thousands of lines of data, such as busy event logs or background processes, just by typing a keyword. You can sort data by clicking column headings or adding criteria filters using the drop-down menus to narrow the results without adding more code.

Be sure to add -PassThru because it turns the grid window into an interactive selection tool. It pauses your script and allows you to highlight specific lines. After you click OK, the window closes and those items are sent back to the pipeline so your script can continue working on them. This is a big step Learning Windows Automation.

Running code on multiple computers

Get more done in less time

Get-Service Select-Object and Invoke-Command in Windows PowerShell Credit: Jorge Aguilar / How To Geek

If you run multiple servers, you know what it’s like to log into each machine one by one for checks or updates. Invoke-Command saves time because it runs code simultaneously on hundreds of remote machines instead of visiting each server one by one. You probably don’t need hundreds of these, but if you have four or five, it saves you a lot of time.

This the team uses remote protocols as WinRM In Windows or SSH for cross-platform installations to support multiple remotes. It sends instructions to each computer on your list and collects the results in your console. When you pass a script block to a cmdlet, it sends your code to all target systems at once. After the remote jobs are done, the results are returned to your local session as objects, so you don’t have to monitor each machine.

Adds PowerShell to help track incoming data PSComputerName property on each object to indicate which server it came from. This gives you a way to group and sort data locally to find differences in your setup.

Find past work using shortcuts

Stop typing the same commands over and over again

# Login to Windows PowerShell Credit: Jorge Aguilar / How To Geek

If you spend time creating a complex PowerShell command, you may realize a few days later that you need the same logic, but you can’t remember the syntax. PowerShell has a command history system that is similar to a record of your past work and allows you to retrieve commands you have previously executed.

As you type a command, PowerShell saves it. Modern environments use internal session history and persistent history PSReadLine manages the module. Although the built-in history only tracks commands in your current session, and PowerShell deletes them when you close the window, PSReadLine records continuously.

This module writes every command you type in all sessions to a file on your machine, so you don’t lose your work between reboots. Instead of retyping long scripts, you can use a keyboard shortcut to pull commands you’ve used before. When you type the pound symbol # followed by a keyword from the command and pressing Tab, PowerShell searches your history for the most recent match. Keep pressing the Tab key until you find what you need. I start with #get and then tab and it usually finds what I need faster.

Track file updates as they happen

Watch for changes as they come

Powershell command with Get-Content Credit: Jorge Aguilar / How To Geek

Running the same command over and over just to see if the log file is updated is annoying. If you want to see a live stream of files written to it by the system, Get-Content cmdlet with -Wait setting is a great way to track it. Normally, Get-Content reads the file and displays its contents to you. However, the cmdlet behaves differently when you add -Wait parameter.

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

You can adjust this by combining -Wait code as -Tail. For example, if you are viewing a very large log file, use -Tail just to show the last few lines before starting to wait. This prevents your console from displaying excessively old data. If you use -TotalCount asking for more lines than there are in the file, Get-Content displays its current contents and waits for the system to write other lines to disk. This helps scripts that need to be stopped until a certain logging event occurs.

Narrow down lists to find specific items

Find a needle in a haystack

It’s hard to find the specific information you need in a large database, but filtering is a great solution. When you encounter too many results Where-Object command truncates lists to show only important elements. It takes output from other commands and restricts that data based on rules you set. You can search for very large files or filter system services to find ones that haven’t started.

The Where-Object A cmdlet is like a gatekeeper in a pipeline that inspects each object and allows only those that match your criteria to move on to the next command. PowerShell allows you to define these rules using a simpler syntax that reads like natural language. This facilitates quick adaptations, such as checking whether the service status is equal to a certain state.

For something more complex, you can use script block syntax with curly braces and variables to test multiple conditions. It allows you to combine operators like this -and -or create custom filters.


Keep these commands handy for future reference

None of these cmdlets require additional installation 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 quickly see how much time you’ve lost in the long run. Just be sure to write them down or bookmark this page so you can come back when you need to.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *