0

In Powershell 5.1 on Windows 10 I am trying to add an alias by calling:

New-Alias -Name k -Value "kubectl -n my-aks-namespace"

However when I try to use it by calling k get pods it seems to me, that all the 3 words in the value of my alias are interpreted as a single string and as such the command "kubectl -n my-aks-namespace" get pods is of course not found by Powershell, resulting in the error:

k : The term 'kubectl -n my-aks-namespace' 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
+ k get pods
+ ~
    + CategoryInfo          : ObjectNotFound: (kubectl -n my-aks-namespace:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

When I just call kubectl -n my-aks-namespace get pods without trying to use the alias, everything works and the list of pods is printed.

UPDATE:

Many thanks for your comments, I have run notepad $PROFILE and put the following line in there:

function k { kubectl -n my-aks-namespace @args }

And now I am able to call the commands like:

k get nodes
k get pods

at my Powershell prompt. (For this to work, I had to remove my broken New-Alias first.)

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 2
    Possible duplicate - https://stackoverflow.com/questions/72998408/trying-to-create-an-alias-to-include-options-in-powershell - summary is you can’t specify parameters in an alias - you need to wrap it in a function instead. The error you’re seeing is because the command you’ve aliased is treating the args as part of the command *name* – mclayton May 20 '23 at 11:02
  • 1
    In short: `function k { kubectl -n my-aks-namespace @args }` – mklement0 May 20 '23 at 20:56

0 Answers0