How to Set Permanent and Temporary Aliases in PowerShell and Bash: A Step-by-Step Guide
Published: 2024-06-19 10:33:56

Aliases are shortcuts that allow you to create custom commands to save time and improve efficiency when working in the terminal. Both PowerShell and Bash support aliasing, but the methods for setting permanent and temporary aliases differ between these shells. In this guide, we'll walk you through the steps to create and manage aliases in both PowerShell and Bash.

### Setting Aliases in PowerShell

#### Temporary Aliases

Temporary aliases in PowerShell exist only for the duration of your current session. Once you close the terminal, these aliases will be lost.

**Creating a Temporary Alias:**

1. Open PowerShell.
    
2. Use the `Set-Alias` cmdlet to create your alias.
    
    ```powershell
    Set-Alias ll Get-ChildItem
    ```
    
    In this example, `ll` is an alias for the `Get-ChildItem` cmdlet, which is similar to `ls` in Unix-based systems.
    

**Viewing Aliases:**

To see a list of all aliases, use the `Get-Alias` cmdlet:

```powershell
Get-Alias
```

#### Permanent Aliases

To make an alias permanent, you need to add it to your PowerShell profile script. This script runs every time a new PowerShell session starts.

**Creating a Permanent Alias:**

1. Open PowerShell.
    
2. Open your PowerShell profile in a text editor. You can do this by running:
    
    ```powershell
    notepad $PROFILE
    ```
    
    If the profile does not exist, this command will create it. You can also use your favorite text editor like VS Code or Sublime text, here I have used `notepad` for simplicity.
    
3. Write the same command in the profile script:
    
    ```powershell
    Set-Alias ll Get-ChildItem
    ```
    
4. Save and close the text editor.
    
5. To apply the changes immediately <mark>without restarting</mark> PowerShell, run:
    
    ```powershell
    . $PROFILE
    ```
    

### Setting Aliases in Bash

#### Temporary Aliases

Temporary aliases in Bash also exist only for the current session. They will be lost once you close the terminal.

**Creating a Temporary Alias:**

1. Open a terminal.
    
2. Use the `alias` command to create your alias.
    
    ```bash
    alias ll='ls -la'
    ```
    
    In this example, `ll` is an alias for the `ls -la` command.
    

**Viewing Aliases:**

To see a list of all aliases, use the `alias` command without any arguments:

```bash
alias
```

#### Permanent Aliases

To make an alias permanent, you need to add it to your shell's configuration file, such as `.bashrc` or `.bash_profile`.

**Creating a Permanent Alias:**

1. Open a terminal.
    
2. Open your `.bashrc` file in a text editor. You can do this with `nano` or `vi`
    
    ```bash
    nano ~/.bashrc
    ```
    
3. Add your alias command in the `.bashrc` file:
    
    ```bash
    alias ll='ls -la'
    ```
    
4. Save and close the text editor.
    
5. To apply the changes immediately <mark>without restarting </mark> the terminal, run:
    
    ```bash
    source ~/.bashrc
    ```
    

### Conclusion

Setting aliases in PowerShell and Bash can greatly enhance your productivity by reducing the amount of typing required for frequently used commands. Whether you need a temporary alias for a quick task or a permanent one for everyday use, the steps outlined above will help you configure your terminal to suit your workflow. Experiment with different aliases to find the ones that best fit your needs. Happy aliasing!