Back

/ 2 min read

Linux aliases in Windows

In linux, you can define aliases for commands. For example, you can define an alias ll for ls -l so that you can type ll instead of ls -l. This is very useful for saving time and typing less.

In windows, I wasn’t sure how to do this for a longtime. Apparently its very easy through Powershell profiles.

What are Powershell profiles

Powershell profiles are scripts that run when you start a powershell session. There are multiple profiles that run in different scenarios. The profiles are stored in the $PROFILE variable.

important commands

Commandwhat it does
Test-Path $PROFILEChecks if you have a profile
New-Item -path $PROFILE -type file -forceCreates a profile if you dont have one
Get-ExecutionPolicyGets the current execution policy
Set-ExecutionPolicy RemoteSignedSets the execution policy to RemoteSigned (by default executions of scripts are not allowed and is set to Restricted
notepad $PROFILEOpens the profile script in notepad

How to create an alias

Its really simple.

  1. Open a powershell session
  2. Run notepad $PROFILE to open the profile script in notepad
  3. Add a function with the syntax function <alias> { <command> }. For example, to add an alias ll for ls -l add the following line to the script and save.
Terminal window
function ll { ls -l }
  1. To pass dynamic arguments to the command, you can use the $args variable within the function.
Terminal window
function grep { Select-String $args }
  1. To add more aliases, add more functions to the script.
  2. Restart the powershell session to apply the changes.

Surely there are many other ways to do this, but this is the simplest way I found so far.