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
| Command | what it does |
|---|---|
Test-Path $PROFILE | Checks if you have a profile |
New-Item -path $PROFILE -type file -force | Creates a profile if you dont have one |
Get-ExecutionPolicy | Gets the current execution policy |
Set-ExecutionPolicy RemoteSigned | Sets the execution policy to RemoteSigned (by default executions of scripts are not allowed and is set to Restricted |
notepad $PROFILE | Opens the profile script in notepad |
How to create an alias
Its really simple.
- Open a powershell session
- Run
notepad $PROFILEto open the profile script in notepad - Add a function with the syntax
function <alias> { <command> }. For example, to add an aliasllforls -ladd the following line to the script and save.
function ll { ls -l }- To pass dynamic arguments to the command, you can use the
$argsvariable within the function.
function grep { Select-String $args }- To add more aliases, add more functions to the script.
- 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.