Add a Bash-like autocomplete to your PowerShell
Tags: powershell, productivity, windows
Published: 2019-12-27 18:54:10

I mostly use Ubuntu or MacOS for development, but recently I switched sides because I needed the power from my Windows gaming machine to start playing around with Blender and Unreal Engine.

What I missed much when using the PowerShell console was the autocomplete ability which Bash offers:

- When you begin to type a command and hit <kbd>Tab</kbd> Bash shows you all available commands which begin with the phrase you typed and you can then select  the one you want with the arrow keys.

    PowerShell by default only completes the command immediately without showing you all available commands.

- If you already started typing the beginning of a command you can use <kbd>↑</kbd> / <kbd>↓</kbd> to cycle through your history to autocomplete the command with the parameters you have used before.

    Cycling through the history with the arrow keys is also supported by PowerShell but it completely ignores what you have already typed and replaces everything with the last used command from the history.


# Enable better autocompletion in your PowerShell

![Autocomplete in PowerShell](https://thepracticaldev.s3.amazonaws.com/i/on38pkqm31hgbldu3ipj.gif)

Thankfully PowerShell already has this functionality built-in provided by a module called `PSReadline`.
You simply have to enable it in your PowerShell profile (The PowerShell equivalent to your `.bashrc`) 👏:

1. Open or create a new [PowerShell Profile](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/how-to-use-profiles-in-windows-powershell-ise?view=powershell-6#selecting-a-profile-to-use-in-the-windows-powershell-ise) by typing the following commands directly in your PowerShell:
    ```powershell
    # Create profile when not exist
    if (!(Test-Path -Path $PROFILE.CurrentUserAllHosts)) {
      New-Item -ItemType File -Path $PROFILE.CurrentUserAllHosts -Force
    }

    # Open the profile with an editor (e.g. good old Notepad)
    ii $PROFILE.CurrentUserAllHosts
    ```

2. In the editor add the following lines to the profile:
    ```powershell
    # Shows navigable menu of all options when hitting Tab
    Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete


    # Autocompletion for arrow keys
    Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
    ```

3. Save the file and reopen your PowerShell, now you can use the advanced autocomplete features.

*Credit for this goes to StackOverflow users [svick](https://stackoverflow.com/a/37715242/831465) and [JerryGoyal](https://stackoverflow.com/a/59334852/831465) which have posted this in their answers.*


# Bonus: Add the `open` command from MacOS to your PowerShell

On MacOS there is the powerful `open` command to open folders in Finder (the file explorer from MacOS) or files with their default application from the terminal.
PowerShell also has this command built-in which is called `ii` (Which stands for [Invoke-Item](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/invoke-item)).
Because I find the term `ii` hard to remember I added an `open` alias for it which makes it more convenient when switching between the platforms.

Adding a new alias to the PowerShell profile is really simple with the [`New-Alias`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-alias) command, so simply edit your profile and add the following line:

```powershell
# New-Alias <alias> <aliased-command>
New-Alias open ii
```

Save it and reopen your PowerShell and test it by typing `open .` to open an Windows explorer window in your working directory.


# Last tip: Use the new Windows Terminal
To make your PowerShell experience even better you should also install the new Windows Terminal preview from the Microsoft store:
https://www.microsoft.com/store/productId/9N0DX20HK701

It offers tabs and a better customization than the default console in Windows.

---


*I am still pretty new to PowerShell so if you have other tips to increase the productivity like this I would love when you leave a comment here.* 💖