Unable to set alias in PowerShell using a string - powershell

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

This will help you create an alias for "php artisan" in Windows PowerShell Globally.
Just follow these steps and its done.
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 }
Then run this command.
notepad $profile
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
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.

All the details are described in:
Get-Help alias -ShowWindow shows all available help for alias
Get-Help Set-Alias show specific help for Set-Alias
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

Related

Windows Terminal doesn't recognize a script function

I've recently discovered the Windows Terminal and wanted to give it a try. However, it doesn't seem to work the same as the original PowerShell Console. When I try to call a script function in it like . .\Func-Test.ps1; My-Func -test abc it says:
My-Func : The term 'My-Func' 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:15
. .\Func-Test; My-Func -test abc
~~~~~~~~
CategoryInfo : ObjectNotFound: (My-Func:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
Doing the same in the classic PowerShell Console works without any issues. Both consoles use the same $PSVersionTable.PSVersion which is 5.1.19041.1682.
Am I using the Windows Terminal incorrectly or isn't this supposed to work in it (yet)?
I fixed it by executing as Administrator this command in the Windows Terminal. Strangely the PowerShell Console already showed RemoteSigned. Apparently they use different environments.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Powershell script running terminals indefinitely

what I'm trying to do is setting up aliases to run Windows Terminal from file browser in the current directory, as admin or regular user.
I'm not very familiar with powershell scripts and I'm trying to understand why my functions are called when the script is loaded instead of when the alias is called, which in turn runs terminals indefinitely..
Here is the script I wrote in $Profile :
$wtHere = "wt -d ."
function Run-Command{
param($Command)
Invoke-Expression $Command
}
function Run-As-Administrator{
param($Command)
Run-Command -Command "runas /user:administrator $Command"
}
set-alias -Name wtha -Value $(Run-As-Administrator -Command $wtHere)
set-alias -Name wth -Value $(Run-Command -Command $wtHere)
Quoting this excellent answer which explains really well what you're doing wrong:
PowerShell aliases do not allow for arguments.
They can only refer to a command name, which can be the name of a cmdlet or a function, or the name / path of a script or executable
And from the official Docs:
The Set-Alias cmdlet creates or changes an alias for a cmdlet or a command, such as a function, script, file, or other executable. An alias is an alternate name that refers to a cmdlet or command.
This should help you understand what is going:
$wtHere = "Hello world"
function Run-Command {
param($Command)
"$Command - Received on {0}" -f $MyInvocation.MyCommand.Name
}
function Run-As-Admin {
param($Command)
Run-Command ("From {0} - Command: $Command" -f $MyInvocation.MyCommand.Name)
}
Set-Alias -Name wtha -Value $(Run-As-Admin -Command $wtHere)
Set-Alias -Name wth -Value $(Run-Command -Command $wtHere)
Get-Alias wth, wtha | Select-Object Name, Definition
You're storing the "result" of your functions and not the functions definitions into your aliases. You can't store an expression such as Run-As-Admin -Command $wtHere as the Value of your Alias, it will be stored as literal, even if you run the alias you would see that PowerShell will run the result of your expression and fail:
wth: The term 'Hello world - Received on Run-Command' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
What you should do is as simple as:
Set-Alias -Name wtha -Value Run-As-Admin
Set-Alias -Name wth -Value Run-Command
Then you can interactively run:
PS /> wtha $wtHere

How to create a Powershell Alias for Set-Executionpolicy

Following guides to create an alias in powershell, I have run the following command:
Set-Alias rush-enable "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass"
This sets the alias. However when I run the alias I hit an error.
PS C:\example> rush-enable
rush-enable : The term 'Set-ExecutionPolicy -Scope Process
-ExecutionPolicy Unrestricted' 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
+ rush-enable
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-ExecutionPo.
..cy Unrestricted:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
However running the command on it's own works fine:
PS C:\example> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
PS C:\example>
Am I missing something? How should I format the command to work as an alias?
As per the documentation:
You can assign an alias to a cmdlet, script, function, or executable
file.
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.
This is presumably a way to avoid having to disambiguate a parameter that exists on an alias and which is passed when invoking the alias.
You therefore have two options:
Encapsulate your command in a function called Enable-Rush (note the PowerShell acceptable naming scheme, if you care). In your case this would look something like:
function Enable-Rush {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
}
Save the command as a file and create a alias to the file.
And to persist it, put it in a $PROFILE. Depending on your system, you might need to digitally sign the $PROFILE or change the PowerShell system Execution Policy to allow PowerShell scripts generated by the system to execute when starting a PowerShell process.

Powershell script cannot recognize absolute file path

I have a windows command script that needs to run another powershell script in another location. The windows command script contains the following code:
powershell -NoProfile -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; & "C:\Users\Tommy\AppData\bootstrap\bootstrap.ps1" -Update"
When I run the windows command script, it always show the following error:
& : The term 'C:\Users\Tommy' 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:53
I have tried adding dot to the front of the absolute file path and it did not work.
Try updating "C:\Users\Tommy\AppData\bootstrap\bootstrap.ps1" to 'C:\Users\Tommy\AppData\bootstrap\bootstrap.ps1'. You are ending your quotes early by using the double quotes again.
powershell -NoProfile -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; & 'C:\Users\Tommy\AppData\bootstrap\bootstrap.ps1' -Update"

Running VMWare powerCLI commands with SoapUI

I have a powershell script file with PowerCLI commands like Connect-VIServer etc. I am able to run the script file in PowerGUI after I added the library references to VMWare PowerCLI but I don't know how to run it through SoapUI. I'm guessing it won't work with the regular Powershell CLI either. Is there any way to make this work? Here's the error, if it helps:
The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At Test.ps1:10 char:23
+ $vm = Connect-VIServer <<<< -Server $vcenterIP -User $vcenterUser -Password
$vcenterPW
+ CategoryInfo : ObjectNotFound: (Connect-VIServer:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
try calling this in the beginning of the script:
Add-PSSnapin "VMware.VimAutomation.Core" | Out-Null
You cannot execute PowerCLI commands outside of a Powershell host.
To add the snap-in to any Powershell host, use the command Tomas mentioned:
Add-PSSnapin VMware.VimAutomation.Core