0

I'm trying to use Power Shell 'Set-Alias' command to make an alias to a complex command with arguments. It doesn't work for anything but simple single commands (for which no alias is needed!). E.g., an alias to 'ls' works, but even one to 'ls *' does not.

PS C:\Users\snyde\BFROOT> Set-Alias

cmdlet Set-Alias at command pipeline position 1 Supply values for the following parameters: Name: tdir Value: ls * PS C:\Users\snyde\BFROOT> tdir tdir : The term 'ls *' 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

  • tdir
  •   + CategoryInfo          : ObjectNotFound: (ls *:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

PS C:\Users\snyde\BFROOT> Set-Alias

cmdlet Set-Alias at command pipeline position 1 Supply values for the following parameters: Name: tdir Value: ls * PS C:\Users\snyde\BFROOT> tdir tdir : The term 'ls *' 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

  • tdir
  •   + CategoryInfo          : ObjectNotFound: (ls *:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

I expect 'tt' to execute 'ls *'

  • Have you tried passing -Option * when setting the alias? I don't use PowerShell much, but I think I've done that in the past. – jwp Aug 10 '23 at 19:41
  • 3
    Instead of an alias you should create a function. `¯\_(ツ)_/¯` – Olaf Aug 10 '23 at 19:44
  • 2
    Not supported, you need to define a function instead – Mathias R. Jessen Aug 10 '23 at 19:51
  • In short: Unlike in POSIX-compatible shells such as `bash`, you _cannot include pass-through arguments_ in a PowerShell alias definition - you need a _function_ for that. PowerShell aliases are simply _alternative names_ for other commands, with no ability to "bake in" arguments. E.g., to define a short command `bar` that invokes command `foo` with fixed argument `baz` while passing all other arguments through, use `function bar { foo baz @args }` – mklement0 Aug 10 '23 at 23:57

0 Answers0