Powershell: Git & .Net Tab Autocomplete Custom CLI
Tags: programming, powershell, windows, microsoft
Published: 2023-12-24 23:09:54

> Coming from WSL and Bash to a full Microsoft stack has had it's ups and downs. One of the more demoralizing moments was losing my really cute Oh My Zsh configured terminal migrating to **Powershell **:'( 

**(tl;dr - [prompt theme engine](https://ohmyposh.dev/) for Powershell or [my custom profile templates](https://github.com/colinwilliams91) for more granular customization with less bloat)**

On _top of that_ neither Git nor .Net have tab-key autocomplete built into their CLIs...

#### BUT THERE IS A SOLUTION!

**Enter: `$PROFILE`**
---
By default, your system will not have the file that is linked to this variable. If the Profile doesn't exist then create an empty text file named `Microsoft.PowerShell_profile.ps1` (note: file extension should be `.ps1`) at your PowerShell installation location e.g. `C:\Program Files\PowerShell\`

I suggest running this command inside Powershell so that your customization will persist wherever you want to use it:

```powershell
code $PROFILE.AllUsersAllHosts
```
_use your desired text editor (e.g. `notepad` if you're a psychopath)_

There are different profiles for different scopes in PowerShell, and they are executed in a specific order. The common profiles include:

`$PROFILE.AllUsersAllHosts`: Applies to all users and all hosts (global profile, console, ISE, etc).
`$PROFILE.AllUsersCurrentHost`: Applies to all users but only for the current host.
`$PROFILE.CurrentUserAllHosts`: Applies to the current user but for all hosts.
`$PROFILE`: Applies only to the current user and the current host.

Inside one of these profile scripts is where you can overwrite your default Powershell `prompt` function. Without any modification it behaves like this:

```powershell
function prompt {
    "$PWD> "
}
```

Here is an example of a very simple `prompt` function I wrote. With this very bare bones set up you can customize the colors of the command line as well as the ASCII characters that are used:

```powershell
function prompt {
  $color = 3, 11, 13 | Get-Random
  Write-Host ("::" + $(Get-Location) + "\:: ~>") -NoNewLine `
   -ForegroundColor $Color
  return " "
}
```
_the only available colors come from the .Net `ConsoleColor` enumeration that I will list below_

```
0:  Black
1:  DarkBlue
2:  DarkGreen
3:  DarkCyan
4:  DarkRed
5:  DarkMagenta
6:  DarkYellow
7:  Gray
8:  DarkGray
9:  Blue
10: Green
11: Cyan
12: Red
13: Magenta
14: Yellow
15: White
```

Within the `profile.ps1` script that you create you can also `Import-Module posh-git` to leverage their really intuitive Git status for the command line. See the Repo Docs/API link below in the References section for full description of how to install and customize. (I also include a step by step walkthrough in my [Github repository](https://github.com/colinwilliams91/profiles) for PS profile templates)

If you wish to explore this further, I made a public repo with some examples, comments, and links to further API and Documentation regarding everything custom PS Profile Scripts! 🐱‍🏍

---
## References:
- Git-Posh Module [Repo Docs/API](https://github.com/dahlbyk/posh-git)
- Oh My Posh [Prompt Theme Engine](https://ohmyposh.dev/docs/)