[LINUX] Creating an alias (when there are multiple arguments) [Comparison between Bash and PowerShell]

Return to Bash and PowerShell command correspondence table

Bash

Command format

$alias alias name="Command name Argument 1 Argument 2...Argument N"

Example of use

As an example, I would like to list an alias for tmux.

#Create alias "ton"
$ alias ton='tmux set-window-option synchronize-panes on'

#Create alias "toff"
$ alias toff='clear; tmux set-window-option synchronize-panes off'

By the way, see the URL below for the meanings of these aliases (ton and toff). Run commands on multiple panes at the same time with tmux [Simultaneous operation of multiple servers with tmux](https://tech.naviplus.co.jp/2014/01/09/tmux%E3%81%A7%E8%A4%87%E6%95%B0%E3 % 82% B5% E3% 83% BC% E3% 83% 90% E3% 81% AE% E5% 90% 8C% E6% 99% 82% E3% 82% AA% E3% 83% 9A% E3% 83 % AC% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3 /)

PowerShell

Command format

does not exist </ font>. Create your own alias with arguments.

The format of the self-made alias function with arguments is quite sloppy, and it is written as follows.

powershell:Microsoft.PowerShell_profile.ps1


function alias name() {
Command name Argument 1 Argument 2...Argument N
}

However, it will not be valid as an alias unless it is described in the file Microsoft.PowerShell_profile.ps1. </ b> </ font>

The location of Microsoft.PowerShell_profile.ps1 is one shot with the following command.

> echo $profile
C:\Users\User name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

If you don't have WindowsPowerShell \ Microsoft.PowerShell_profile.ps1 under the Documents folder, you can create your own ** folder </ font>.

Example of use

As an example, I would like to list an alias that launches Word from PowerShell.

powershell:Microsoft.PowerShell_profile.ps1


#Open word
function word() {
    $WORD_PATH = "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

    if ( $args ) {
        #If you have a file you want to open
        Start-Process $WORD_PATH $args
    }
    else {
        #If you just want to start Word
        Start-Process $WORD_PATH
    }
}

![003.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/614920/e9f6b0cd-0cfc-8a27-1e86-ca30264df0cf.png)

After creating the alias, load Microsoft.PowerShell_profile.ps1 with. (Dot) .

$ Profile is Microsoft.PowerShell_profile.ps1! !!

> . $profile    # 「.Read with (dot) "

If you can create the alias properly, just execute word in PowerShell and Word will start.

word_alias.gif

If you want to separate it as an alias-only file

[Click here to open] Separate as an alias-only file
  • First, create a file dedicated to the alias.
    Here, create powershell_aliases.ps1 under the same folder as Microsoft.PowerShell_profile.ps1.

000.png


  • Next, open Microsoft.PowerShell_profile.ps1.
> vim $profile

#If you don't have vim, open it with "notepad"! !!
> notepad.exe $profile

  • Have Microsoft.PowerShell_profile.ps1 load the alias-only file powershell_aliases.ps1.
    Specify the location of the alias-only file with an absolute path **. </ font>

powershell:Microsoft.PowerShell_profile.ps1


#Reading an alias-only file
$ALIASES = "C:\Users\User name\Documents\WindowsPowerShell\powershell_aliases.ps1"
. $ALIASES    # 「.Read with (dot) "

001.png


  • Create an alias of your choice in the alias file powershell_aliases.ps1.
> vim "C:\Users\User name\Documents\WindowsPowerShell\powershell_aliases.ps1"

#Or open with notepad

> notepad.exe "C:\Users\User name\Documents\WindowsPowerShell\powershell_aliases.ps1"

002.png


  • Finally, load Microsoft.PowerShell_profile.ps1 with. (Dot) to reflect the alias.
    Load Microsoft.PowerShell_profile.ps1 instead of powershell_aliases.ps1.
> . $profile    # 「.Read with (dot) "

the end.