Aliases and Small Functions

Set-Alias gives a command a short name of your own, and when the job needs a parameter's value or more than one step, a small function does it instead; both go in your profile so they're there in every window.

Apprentice · PowerShell · Updated

After this lesson you can

  • Make a short name with Set-Alias, and check first with Get-Command that the name is free
  • Choose between an alias and a small function, and write the function when the job needs a value or more than one step

Key takeaways

  • Set-Alias -Name touch -Value New-Item makes a short name of your own. Get-Command touch first tells you whether the name is free.
  • An alias is only another name for one command: it can't carry a parameter's value or a second step.
  • A function is a name for a block of commands in { }. param($Path) names the word typed after it.
  • Aliases and functions typed at the prompt last until the window closes. Put them in your profile to keep them.

How many times a day do you type Set-Location "$HOME\terminal-practice"? Or make a new folder and then go into it, two commands, every time? Each one is only a few seconds, but they add up, and long commands are where typos live. Developers and IT techs keep a small set of short names for the jobs they do most. This lesson shows you how to make your own, and how to tell which kind of short name a job needs.

This lesson works in a practice folder of its own. Make it and go there:

New-Item -ItemType Directory -Force -Path "$HOME\terminal-practice\shortcuts" | Out-Null
Set-Location "$HOME\terminal-practice\shortcuts"

They print nothing when they work.

Your own alias: Set-Alias

Short Names: Aliases showed PowerShell's own nicknames, such as ls for Get-ChildItem. You can make your own. On a Mac and Linux, touch makes an empty file; let's give New-Item that name. First check that the name is free, so you don't hide a command that's already there:

Get-Command touch
Get-Command: The term 'touch' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Here, red is good news: nothing is called touch. Set-Alias makes the alias: -Name is the short name, -Value the command it stands for.

Set-Alias -Name touch -Value New-Item
touch shopping-list.txt
    Directory: C:\Users\Sam\terminal-practice\shortcuts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           11/9/2026  6:16 AM              0 shopping-list.txt

Run it a second time and New-Item says the file already exists: it never replaces one. Get-Alias touch now shows touch -> New-Item. Another favorite: Set-Alias -Name np -Value notepad, and np shopping-list.txt opens the file in Notepad.

What an alias can't do

An alias is a nickname for one command, and nothing more. Try to give it a value too:

Set-Alias -Name practice -Value "Set-Location $HOME\terminal-practice"
practice
practice: The term 'Set-Location C:\Users\Sam\terminal-practice' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PowerShell looked for one command whose whole name is Set-Location C:\Users\Sam\terminal-practice, and there isn't one. Remove the broken alias with Remove-Alias practice. (Windows PowerShell 5.1 has no Remove-Alias: use Remove-Item Alias:practice.)

A small function

When the short name needs a value or a second step, write a function: a name for a block of commands. Read it as "function, its name, then what it does, inside curly braces":

function practice { Set-Location "$HOME\terminal-practice" }
practice
Get-Location
Path
----
C:\Users\Sam\terminal-practice

A function can also take a word, like a command takes a parameter. param($Path) names it, and the function uses it as $Path. This one makes a folder and goes into it, two steps in one word (Parameters: param(), in Module 6, does much more with param):

Set-Location "$HOME\terminal-practice\shortcuts"
function mkcd {
    param($Path)
    New-Item -ItemType Directory -Path $Path -Force | Out-Null
    Set-Location $Path
}
mkcd "trip photos\day 1"
Get-Location
Path
----
C:\Users\Sam\terminal-practice\shortcuts\trip photos\day 1

So: one command under a new name, an alias. A value, or more than one step, a function.

Keep them in your profile

Aliases and functions you type at the prompt last until the window closes. To have them every day, add the same lines to your profile (edit $PROFILE, after keeping a copy as Your Profile shows), save, and load it with . $PROFILE. Get-Command practice, mkcd, touch then shows all three ready, as two Functions and an Alias.

Pick names that say what they do, and keep them short: you'll type them hundreds of times.

Try it in the terminal

Terminal Quest: The Clockwork Workshop, goal 5: played right on the lesson page.

Winding the clock takes a long command with two options every time. An alias can't hold them: give it a short name with a function.

Check yourself

2 questions to try on the lesson page.

So what

The commands you type most deserve a short name: an alias when it's one command, a small function when it needs more, and your profile to keep them.

Key terms

Cheat sheets

Related