PowerShell Tips & Tricks That Will Increase Your Productivity

Updated: Sep 11

Last week, I shared 8 Quick and easy tips to get you started with PowerShell on Reddit and I received amazing feedback, some of which were mind-blowing tricks that I either did not know or did not use often enough. So I decided to create this post to keep track of all those wonderful shortcuts PowerShell has to offer.

Whether you use PowerShell daily or even occasionally, these tips will make you insanely more productive.

I'm going to use this post to track all the mind-blowing PowerShell tricks I come across, so make sure to check it regularly, and let me know what I missed.

CTRL+SPACE

This one's usefulness is out of this world. If you're like me, you've probably used the PowerShell tab autocomplete to find the cmdlet, parameter, or variable you're looking for. Well, let me blow your mind with CTRL+SPACE. This shortcut will display all the available options AND let you choose the one you want using the arrow keys. 🤯🤯🤯


List all console shortcuts

If the previous one blew your mind, get ready for more! Get-PSReadLineKeyHandler will display all the keyboard shortcuts for the PowerShell console.

Get-PSReadLineKeyHandler


Set your own console shortcuts

Let's take it one step further with Set-PSReadLineKeyHandler, which allows you to set your own console shortcuts! Here's an example of how you can associate your own script to key binding:

# Associates Ctrl+Shift+F (upper case F) to insert a Where-Object block and sets the cursor in between the two brackets
Set-PSReadLineKeyHandler -Chord Ctrl+F -ScriptBlock {
   [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' | Where-Object{')
   [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
   [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
   [Microsoft.PowerShell.PSConsoleReadLine]::Insert('}')
   [Microsoft.PowerShell.PSConsoleReadLine]::BackwardChar()
   [Microsoft.PowerShell.PSConsoleReadLine]::BackwardChar()
}      

Aliases

Cmdlets can be rather tedious to type, especially when performing short operations. Thankfully, PowerShell has shorter versions of cmdlets, or even specific properties, a.k.a. aliases. Aliases can significantly reduce the amount of typing you have to do. My top two favorite aliases are % (ForEach-Object) and ? (Where-Object). Other aliases include UNIX and DOS command aliases such as ls and dir, both of which map to Get-ChildItem.

Note: Aliases are nice to use or setup in the console, but it's generally considered bad practice to use them in scripts. They make it harder to troubleshoot and read. So only user them in the console.

If you want a full list of available aliases, just enter 


Get-Alias

Write Aliases, Save Cmdlets

I know I said you can use aliases to speed things up, but as I also mentioned, you should not save them in scripts. So, now the real question is: How can be lazy AND not lazy at the same time? Well, if you use Visual Studio Code, which you should, you can configure the PowerShell extension to replace all your aliases in your code when you press Alt+Shift+F. There are also extensions for PowerShell ISE, but since it is no longer in development, I won't cover that.



$?

This is my best trick when coding. Some times try/catch is too overkill. Sometimes, I just want to know if the previous action ran without error, without getting into a whole try/catch setup. $? does just that! It returns $true if the previous step ran without error and $false if it did. That's a quick and simple way to handle errors, without setting up try/catch.

Note: For executable success, you should use $LASTEXITCODE instead

Get-Process chrome -ea SilentlyContinue                                                     if ($?){
    Write-Host "Chrome is running!"
}else{
    Write-Host "Chrome is not running" -ForegroundColor Red
}                     

$PSDefaultParameterValues

Do you find yourself entering a cmdlet with the same parameters over and over again? Do you want to see if something will work, but you don't want to do anything? Well, $PSDefaultParameterValues is for you! It allows you to set specific parameter values for some or all cmdlets. You could, for example, set all the cmdlets to run with -verbose or -whatif, without needing to specify the parameter anywhere other than in $PSDefaultParameterValues explicitly.


$PSDefaultParameterValues.add("Remove-Item:WhatIf",$true)

Recent Posts

See All

Tutorial Tuesday - Atom Installer

Hi there everyone, Today we are launching a new series called Tutorial Tuesday. In this series, we'll be showing you what you can do with Koupi. We built Koupi because we wanted a tool that was both a

©2019-2020 by Bulles Technologies. Proudly created with Wix.com