'Unable to set alias in PowerShell using a string

I tried to set an alias in PowerShell by running Set-Alias -Name artisan -Value 'php aritsan', though the command ran successfully but when I call the alias the following error occurs :

artisan : The term 'php aritsan' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ artisan
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (php aritsan:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

What's the correct way to go for?

P.S: artisan is a file in the current directory. The file is packaged with laravel framework



Solution 1:[1]

All the details are described in: get-help about_Aliases -ShowWindow

An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any Windows PowerShell commands.

You can assign an alias to a cmdlet, script, function, or executable file. However, you cannot assign an alias to a command and its parameters. For example, you can assign an alias to the Get-Eventlog cmdlet, but you cannot assign an alias to the "Get-Eventlog -LogName System" command.

However, you can create a function that includes the command. To create a function, type the word "function" followed by a name for the function. Type the command, and enclose it in braces ({}).

For example, the following command creates the syslog function. This function represents the "Get-Eventlog -LogName System" command:

function syslog {Get-Eventlog -LogName System}

You can now type "syslog" instead of the command. And, you can create aliases for the syslog function.

For more information about functions, type:

Get-Help about_Functions

Solution 2:[2]

This will help you create an alias for "php artisan" in Windows PowerShell Globally. Just follow these steps and its done.

  1. First of all search powershell using win+S and run it as administrator and then paste this code and hit Enter.

    if (!(Test-Path -Path $PROFILE )) {New-Item -Type File -Path $PROFILE -Force }

  2. Then run this command.

    notepad $profile

  3. A notepad file will open. Paste this code in notepad file and hit ctrl+S

    Function CD32($arg1,$arg2,$arg3,$arg4,$arg5) {php artisan $arg1 $arg2 $arg3 $arg4 $arg5} Set-Alias -Name pa -Value CD32

  4. And then finally paste this command on your powershell and hit enter.

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

And it's done. Now you can use "pa" as an alias for "php artisan" globally in your windows powershell.

Here I am using "pa" on my VS Code PowerShell.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivan Mirchev
Solution 2