UPDATED-STATEMENT: 20190815@000000@THU Working on creating a powershell initial profile for current user only and without running as admin that is authorized to run commands from from the ps-profilefile such as custom aliases that isn't cmlet specific. UPDATED-STATEMENT: 20190825@112626@SUN Created working powershell initial profile-file that is only for current user and is authorized to run scripts via setting execution policy as "RemoteSigned" but custom aliases don't work. Only alias for notepad works. UPDATED-STATEMENT: 20190901@171442@SUN Still no way to run an executable with cmd like params or arguments without error. UPDATED-STATEMENT: 20190809@171100@SUN Removed the "%" ps alias for "ForEach-Object" and instead used just one parameter but ps doesn't recognize parameters. How do I get powershell to recognize parameters? UPDATED-STATEMENT: 20190915@105906@SUN Bounty ended no solution so far.
SPECIFICATION
PS C:\Windows\System32\WindowsPowerShell\v1.0> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 18362 145
I want to be able to use Powershell instead of Command Prompt. In order for me to do so I need to set up Aliases that are NOT cmdlets OR something as basic like notepad.exe. The aliases I want to define are from executables with parameters that I find to be too much to type for a repetitive task. Below is an alias I'm trying to achieve, but can't get it to work. This custom alias is added within PS as session only for demonstration purposes (once it works then i'll add it to the currentuser ps-profilefile) So, my question is: how do I define an alias that isn't a cmdlet, but an executable with at least one parameter or more?
Are Aliases defined in PowerShell a shorter way to query Cmdlets? Because that's the most I get from all the documentation that I've come across.
RUNNING ALIAS BY DEFINING FUNCTION
CODELINE1_setfunction:
[OLD] PS C:\Windows\System32\WindowsPowerShell\v1.0> function FB {pathto/foobar.exe -parameter1 %%(option1)s.%%(option1a)s -parameter2 --option2}
PS C:\Windows\System32\WindowsPowerShell\v1.0> function FB {pathto/foobar.exe -parameter2 --option2}
CODELINE2_definealiaswfunction:
PS C:\Windows\System32\WindowsPowerShell\v1.0> set-alias FOOBAR -value FB
CODELINE3_runalias:
PS C:\Windows\System32\WindowsPowerShell\v1.0> FOOBAR
[OLD] FOOBAR : The term 'FOOBAR' 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 + FOOBAR + ~~~~~~~ + CategoryInfo : ObjectNotFound: (FOOBAR:String) [], CommandNotF oundException + FullyQualifiedErrorId : CommandNotFoundException
New-Item : A parameter cannot be found that matches parameter name ''. At C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:1 char:47 + new-item alias:aliastofoobar -value foobar.exe -parameter2 --option1 + ~~ + CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
RUNNING ALIAS BY SCRIPT W/ NOTEPAD FOR SIMPLICITY
- Do you have a script that can run commands?
- Yes, pls. see script below for content & the it's error upon ps launch
PS C:\Users\user> $PROFILE C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 PS C:\Users\user> test-path $PROFILE True PS C:\Users\user> Get-ExecutionPolicy RemoteSigned
new-item alias:npa -value notepad.exe /A new_ansifile.txt
New-Item : A positional parameter cannot be found that accepts argument '/A'. At C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:1 + new-item alias:np -value notepad.exe /A new + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
SOLUTION
- None-so-far
%
, which is the alias forForEach-Object
, and why are you escaping it using%%
? What do you have inoption1
andoption1a
?New-Item : A parameter cannot be found that matches parameter name '<parameter2>'.
is there any way to have ps recognize parameters to an executable?