2
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
7
  • What's wrong with writing a PS script and passing arguments to it when you call it?
    – DavidPostill
    Commented Aug 11, 2019 at 8:11
  • Is PS Scripting a position-file (or script.ps1) that has commands regardless of 1-liners being executed by running the script within powershell? This seems far from reach if I was in a different directory. So in this case to run an alias via ps-script I have to give the path name to the script to run it when i'm in a different directory.
    – fohrums
    Commented Aug 11, 2019 at 10:20
  • You can add the directory containing your powershell scripts to you PATH ...
    – DavidPostill
    Commented Aug 11, 2019 at 11:10
  • I can't understand the function FB: Why do you use %, which is the alias for ForEach-Object, and why are you escaping it using %%? What do you have in option1 and option1a?
    – harrymc
    Commented Sep 7, 2019 at 20:23
  • I see as it says so immedidiately. I removed it and decided to use one parameter instead (by now updated my ps-profile) and noticed that ps is needing the parameter(s) used to be defined. E.g. New-Item : A parameter cannot be found that matches parameter name '<parameter2>'. is there any way to have ps recognize parameters to an executable?
    – fohrums
    Commented Sep 8, 2019 at 7:10

2 Answers 2

1

If I'm understanding what you're looking to do, then yes, totally doable.

Wrap your foobar.exe call in quotes to make it all a single string.

"foobar.exe -param1 -param2"

In your function, put cmd /c before the string. https://stackoverflow.com/questions/515309/what-does-cmd-c-mean

Then, use Set-Alias to set it how you like, with -Value as the function name.

All together...

function doSomething() {
    cmd /c "foobar.exe -param1 -param2"
}

Set-Alias -Name FOOBAR -Value doSomething

Now, when FOOBAR is ran in PS, it executes foobar.exe -param1 -param2 as if you typed it manually.

If I misunderstood the question, please let me know. Hopefully this helps.

0

With the very same PSVersion I get different results:

PoSh> function FB {pathto/foobar.exe -parameter1 %%(option1)s.%%(option1a)s -parameter2 --option2}
PoSh> (Get-Command FB).Definition
pathto/foobar.exe -parameter1 %%(option1)s.%%(option1a)s -parameter2 --option2
PoSh> Set-Alias FOOBAR -Value FB
PoSh> Get-Alias FOOBAR

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           FOOBAR -> FB

So it's neither the definition of the function (whose script block is evaluated on execution) nor the definition of the Alias what provokes the error you describe.

It's more likely the error is about quoting, wrong parentheses or arguments at the time the script block is executed.

2
  • I believe your referring to get-help about_execution_policies. So running powershell as non-admin and initiating profile by New-Item -path $profile -type file –force with an alias for notepad won't run due to the execution policies. How do I get an alias for notepad to run without running ps as admin?
    – fohrums
    Commented Aug 24, 2019 at 14:55
  • After some time, I figured how to run local profile-files as non-admin. First, set execution policy to not use Restricted, instead to use RemoteSigned: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser problem is the alias for notepad works, but not the alias with an executable with parameters. Why is that? I'm beginning to think that PS is only able to create aliases for cmdlets or built-in Windows proccesses like notepad.exe. I still need help creating a custom alias.
    – fohrums
    Commented Aug 25, 2019 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .