Sunday, June 10, 2018

PowerShell aliases can't include parameters

I recently helped a user who was having some trouble with PowerShell aliases. He was trying to define a handful of aliases, each passing a different parameter to a function. That was a problem because PowerShell aliases are just alternate names for commands; they cannot specify parameters. (That's why, for example, mkdir is a function that has to do some fancy stuff to wrap New-Item, specifying -ItemType Directory but passing the other parameters straight through.) To work around that, you can use Invoke-Expression to run commands that create simple functions to call the more general function.

iex "function $NewFuncName {SomeFunction $TheParameter}"

No comments:

Post a Comment