Alias Commands on Windows
Previously, I spelled out how to alias ssh command in a manner which is specific to a system running Bash — Ubuntu specifically. Windows’ PowerShell environment requires a different specifics, though luckily it is the same general approach.
The general idea is this: Each time a shell environment such as PowerShell or Terminal is opened, a set of file scripts are run which initialize what functions are available to the session. In the case of a standard Ubuntu distribution, .bashrc is one of those scripts. Adding the alias command to .bashrc makes the named alias available to the developer.
Migrating this concept to Windows requires identifying the shell’s start script file and the way to add a new function that will be persisted during the session.
In PowerShell, the start script file is kept in shell variable $profile , and the built in function to create an alias is Set-Alias [aliasname] [cmd].
For one-word aliases, this can be used as a one-liner:
> echo "Set-Alias alias cmdlet" >> $profile
Practical Use Of Set-Alias
If you’re anything like me, neither the path nor file which the $profile variable holds exists at first, so a new folder and file must to be created for it. Creating the directory can be accomplished using the Split-Path cmdlet, specifying the $profile file.
> mkdir (Split-Path $profile)
The echo command can be used to create the file.
> echo "Set-Alias alias cmdlet" >> $profile
Anything more complicated than aliasing one word for another will require the steps to be contained within one function, the name of which will be used as cmdlet .
function multistep-process {
p1
p2
}
Set-Alias alias multistep-process
You will need to restart PowerShell for the change to take effect. A way to refresh the shell without closing it is to run . $profile
> . $profile
To put it all together, and with a practical example, here is adding an ssh alias to Windows PowerShell.
> mkdir (Split-Path $profile)
> (echo "function ssh-remote { ssh alexa@remote.host }; Set-Alias remote ssh-remote" >> $profile) -and (. $profile)
# Now the command "remote" is available for use
> remote
Using File Explorer
I’ve detailed how to go about doing all of this using the command line because of the audience I expect to be asking a question like this — developers who spend time in PowerShell. The general PC owner, though, will likely feel more comfortable navigating files using file explorer and notepad. All of this can be accomplished in such a manner.
You will need to know what file is stored in the $profile variable to edit in the correct place. The file location can be seen by typing $profile in the command line. From there, open file explorer and create folders and files as usual, being sure to name them exactly as spelled in the variable. The file can be opened and edited using notepad.
Final Thoughts
There’s more than one way to do things, especially on Windows given the different command line programs — powershell, cmd, git-bash to name the most common. This covered how to do it in PowerShell.
I spent some time trying to find an elegant solution which stayed in one shell session and used the $PsHome start script file, but it does not appear possible. If you find yourself starting to type runas /user:administrator, come back to the instructions as written here.
That should be enough information to get you started, and for sure that’s one way that will get an alias set on Windows.
Written: April 2, 2024
Last updated: September 23, 2024