Windows Aliases for Terraform and Git

Ever wonder how to easily create new Windows aliases for Terraform, git, or other tools? This post is for you!

I prefer to use the CLI when working on Infrastructure as Code projects using tools such as git and Terraform. The CLI experience feels speedy and clean when compared to loading a client to navigate an interface. I’m also a creature of habit. However, there are times when the existing set of CLI commands fall short of my needs and I create new helpful aliases.

Windows Aliases for Terraform - SpongeBob Style!
Helpful aliases in the wild.

Recently, I was asked about creating aliases for common Terraform commands running on Windows. As a fellow Windows user myself, I wanted to share a few tips on the subject. In this post, I’ll explain how to create on-demand aliases using the PowerShell profile along with some examples used in my development environment for git and Terraform.

Quick Reminder: I livestream on technical topics every week on Twitch – come join the adventure! Drop a follow and enable notifications to stay current.

PowerShell User Profile

In a previous post entitled How to Configure a PowerShell ISE Theme and User Profile, I went through setting up a user profile for numerous reasons. A few things have changed since then. Today, I am running Windows 10 on a laptop with PowerShell Core 7.0.2 installed. I’ve completely abandoned PowerShell for Windows.

This means that my user profile is located in my OneDrive folder in the path shown below:

C:\Users\chris\OneDrive\Documents\PowerShell\profile.ps1

The user profile is loaded at the start of a PowerShell session, which is my default CLI. Loading aliases into the user profile is a great idea. It gels nicely with stand-alone PowerShell prompts and integrated VS Code prompts. If this file is missing, create one.

I’m now ready to make new aliases in the profile.ps1 user profile. I tend to make two types of aliases: commands and functions. I’ll cover both of these types of aliases in the next sections.

Windows Command Aliases

A command alias directly references another existing command. For example, the alias below takes terraform, which is a long command name, and shrinks it down into tf – something less annoying to type!

# Terraform 0.12.x
New-Alias -Name "tf" -Value "terraform"

Command aliases are helpful when testing new versions of a command. For example, I use an alias to switch between Terraform version 0.12 and 0.13.

# Terraform 0.13.beta
New-Alias -Name "tf13" -Value "terraform13b3.exe"

As long as the command is located somewhere in the path environmental variable, no other work is required. If the command requires parameters or additional arguments, this method requires that you type them in manually after the alias. To remedy this shortcoming, point the alias at a function.

Windows Function Aliases

A function alias provides a short function to execute when the alias is called. This comes with several advantages, such as the ability to pass complex arguments, leveraging environmental variables, and combining multiple steps (aliases, functions, etc.) into one alias.

For example, I recursively format Terraform files before making a commit. Instead of typing tf fmt -recursive each time, I use a function named New-CWTerraformFormat that combines the tf alias with the recursive format parameter. I then use an alias named tff to refer to the function.

# Terraform 0.12.x
New-Alias -Name "tf" -Value "terraform"

# Terraform Format Recursive
function New-CWTerraformFormat { tf fmt -recursive }
New-Alias -Name "tff" -Value New-CWTerraformFormat

Inserting something unique into a function name to avoid conflict with other cmdlets. I often use CW, my initials, to make determining who to blame easier. 🙂

A similar tactic exists for git commands. In this example, I create two functions to handle adding untracked files and creating a new commit. The second function, Add-CWGitCommit, uses the $args variable to allow for a custom commit message when run.

function Add-CWGitAll { git add . }
New-Alias -Name "gita" -Value Add-CWGitAll

function Add-CWGitCommit { git commit -s -m $args }
New-Alias -Name "gitc" -Value Add-CWGitCommit

I am then able to use gitc "A wonderfully crafted commit message!" to entertain my fellow collaborators.

If desired, multiple functions can be chained together. I don’t use this method because I find having aliases for each step in the development process to be the desired state.

Next Steps

Please accept a crisp high five for reaching this point in the post!

If you’d like to learn more about Infrastructure as Code, or other modern technology approaches, head over to the Guided Learning page.

If there’s anything I missed, please reach out to me on Twitter or catch my next Twitch live stream. Cheers! 🙂