Logo

dev-resources.site

for different kinds of informations.

Quick guide to setting up an shortcut/alias in windows

Published at
7/1/2024
Categories
productivity
automation
windows
powershell
Author
minhaz1217
Author
10 person written this
minhaz1217
open
Quick guide to setting up an shortcut/alias in windows

This is a quick 30 second guide to setting up an alias in windows. All this is applicable for windows.

Motivation

The main motivation behind this is that I wanted to simplify the ping command. So that I'll just type a single command and it'll execute the ping www.google.com -t, which I use to check if my internet connection is stable or not.
The motivation behind this post is that I couldn't find a quick guide/overview to setup alias or terminal shortcuts when I searched google. So hopefully this will be helpful to juniors to get started.

Open up a powershell and type away.

Basic alias by creating simple shortcut.

If you want to just create an alias for a command the script is like this.
If I want to reference the command echo using a shortcut like ec the command is like this

Set-Alias -Name ec -Value echo
Enter fullscreen mode Exit fullscreen mode

Now the ec command will refer to the echo command, and the echo command is there as well, it didn't go anywhere. To try and see if this works you can try this

ec "Hello World"
Enter fullscreen mode Exit fullscreen mode

The ec command now is just another command to refer to echo

Little bit extended - call simple commands with shortcut

To create the final alias, I'll have to use a powershell function like this.

Function PingGoogle { ping www.google.com -t }
Enter fullscreen mode Exit fullscreen mode

Here the function's name is PingGoogle and it executes the ping www.google.com -t command. We can try it by just entering the PingGoogle command after pasting the method.

Now I can start a ping by calling this method

Now we'll have to make an alias to this command. Like this

Set-Alias -Name pingg -Value PingGoogle
Enter fullscreen mode Exit fullscreen mode

Here the pingg alias is referring to the PingGoogle function. To test this we can just use pingg command now.

now everytime I type pingg the ping will run

Bonus - How to persist these changes.

You'll notice that if you close your terminal then the changes go away, your shortcut won't work.
So to persist these changes across all terminals you'll have to save them in your terminal profile.

To do that type this

echo $profile
Enter fullscreen mode Exit fullscreen mode

This will show you the location of your powershell profile file. Go to that location and open that file.
Or open that file with this

notepad $profile
Enter fullscreen mode Exit fullscreen mode

A file will open in notepad.

Now paste your code in that notepad and save in the same location. Now what will happen is that every time your terminal runs it will also load codes from this file. That's why your changes that your pasted here will persist between sessions.

Reference

  1. Microsoft Documentation
  2. My own notes
powershell Article's
30 articles in total
Favicon
Windows ไธŠ VSCode ็š„ C/C++ ๅปถไผธๆจก็ต„่™•็†็ทจ็ขผ็š„ๅ•้กŒ
Favicon
PowerShell ็š„ๆ–‡ๅญ—็ทจ็ขผ
Favicon
็ฌ”่ฎฐ3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
โ“ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! ๐Ÿš€
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: