How to create shortcuts in PowerShell


Find yourself repeatedly manually accessing often used folders by using cd and tab completion? For instance when you open a new console and have to access a file or folder like the repo folder or the hosts file. Instead of typing the path each time. I found that creating simple aliases for paths I often use spares a lot of punching.

In this post I go through a bit about creating aliases in PowerShell to improve efficiency and minimize typing, and by showing some simple examples of what the PowerShell Profile can be use for.

Different ways of adding aliases

There are different ways of creating aliases and different scopes you can create them in.

One way is to create an alias in the current PowerShell window, but if you create an alias in a PowerShell session the alias will only exist in the current session and will be gone if you close the terminal and open up a new one. This is not very useful and effective if you must recreate the aliases for each console you open.

So, what we want is to have the aliases available at all time, by making them persistent. This is done by creating the alias in the PowerShell profile. Each time when you open up a new console, the Profile script runs and gets loaded into memory, and the aliases is available.

How do we add aliases to the PowerShell Profile?

To add the aliases to the profile, we need to find the Profile path.

This can be done by executing the Environment variable $Profile in the console, like below.

PS C:\> $Profile
Looking up the filepath for the PowerShell Profile

This gives you the path where the profile is located, if the file path is not created from before. You can check if the path exists by running the command below.

# Test if the Profile path exists
PS C:\>Test-Path -Path $Profile

# If it returns true, the file exists
Testing if the PowerShell Profile exists

If the command returns false, then you need to create the path for the profile. This can be done by running following command.

# If not, create it with
PS C:\>New-Item -Path $Profile -ItemType File

To test if the Path was created successfully, you can run the following command.

# To check if it was created run
PS C:\>Test-Path -Path $Profile

When you have verified that the profile exists, you can simple open the profile by typing this:

NOTE: This must be runned with administrator privileges

# Open the Profile with
PS C:\>Notepad $Profile

Then it remains to add some aliases to the profile.

In this post we only focus on a few aliases, but as soon as you catch yourself typing the same thing over and over again, I recommend creating an alias to save time. For instance if you type notepad a lot, creating an alias named np is a lot faster to type.

So let’s move on, the profile may look something like below; in the script I create functions that executes the desired commands and then refers to them in the Set-Alias cmdlet.

function Set-LocationRepos {
    Set-Location C:\Users\$env:USERNAME\source\repos
}
function Invoke-Profile {
    Notepad.exe $Profile
}
function Invoke-Hosts {
    Notepad.exe C:\windows\system32\drivers\etc\hosts
}
function New-File([string]$Name) {
    New-Item $Name -ItemType file
    }

Set-Alias hosts Invoke-Hosts
Set-Alias np c:\windows\system32\notepad.exe
Set-Alias pp Invoke-Profile
Set-Alias repos Set-LocationRepos
Set-Alias touch New-File

After adding the aliases to the profile, save it. Then close the current console and open a new one. When you open the new console, the script will get executed and the new aliases should now work.

You should be able to type hosts (to open the hosts file), np (to open Notepad), pp (to open the PowerShell Profile) and repos (to go to the repos folder, used with Git), after adding the aliases above.

If you want to view all the predefined and newly created aliases in PowerShell you can type the following

PS C:\>Set-Location Alias:

#The console will show the drive as
PS Alias:\>

#Then type the following to view all aliases
PS C:\>Get-ChildItem *

The command will open the Alias folder and you will get a list of all aliases that exists, including the newly created. If you want to export all aliases and use them on another computers or other PowerShell consoles, for instance PowerShell ISE and Visual Studio Code, you can use the following commands:

# For exporting all aliases to file
PS C:\>Export-Alias C:\MyAliases.txt

# To import all the aliases
PS C:\>Import-Alias C:\MyAliases.txt

If you have created functions that reference the Set-Alias cmdlet an easier way to export your own created aliases is to export the profile. This can be done by listing the content of the $Profile and copy the content to the desired destination.

# This will only apply for the PowerShell console, not PowerShell ISE or VSCode.
PS C:\>Cat $PROFILE > C:\Microsoft.PowerShell_Profile.ps1

# Copy the content from the exported file and into the profile.
PS C:\>Copy-Item -Path C:\Microsoft.PowerShell_Profile.ps1 -Destination $PROFILE

That was a bit about creating aliases in PowerShell, hope you enjoyed.


For More Content See the Latest Posts