DevOps | Scripts | Automation

PowershellTroubleShooting

How to: The term ‘functioname’ is not recognized as the name of a cmdlet ? in PowerShell?

Generally, this error occurs with function name in PowerShell, when PowerShell can’t recognize the name of the functions. There can be few reasons behind it.

Function Name Declaration:

First, the function name is declared before it was called. The function name should be loaded first into the PowerShell memory and then they should be called. In the below example, output generates an error because it can’t recognize the name of the function.

$Animals = "Dog","Cat","Tiger"

foreach($animal in $Animals){
  printAnimals($animal)
}

Function PrintAnimals($animal){
   Write-Output "Animal : $Animal"
}

Output:

Actual program should be as shown below.

$Animals = "Dog","Cat","Tiger"

Function PrintAnimals($animal){
   Write-Output "Animal : $Animal"
}

foreach($animal in $Animals){
  printAnimals($animal)
}

Output:

Undeclared ‘Function’ Keyword:

Even if you put the function before it loads and if you forgot to use the function keyword ahead of function name, it will generate and error. For example,

$Animals = "Dog","Cat","Tiger"

PrintAnimals($animal){
   Write-Output "Animal : $Animal"
}

foreach($animal in $Animals){
  printAnimals($animal)
}

Output:

In the above example, function should be declared as,

function PrintAnimals($animal)