The term is not recognized as the name of a cmdlet function PowerShell

The term FunctionName 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.

Recently while working on a PowerShell script to copy documents from a local drive to SharePoint Online document library, I got an error which says:

The term ‘LogToSharePoint’ 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.

Here “LogToSharePoint” is my function name. The full error looks like below:

the term is not recognized as the name of a cmdlet function powershell
The term FunctionName is not recognized as the name of a cmdlet function script file or operable program

The function was like below:

Function LogToSharePoint($title, $description)
{
#Here was my code to save an item to a SharePoint online list.
}

Ans I was calling like below:

LogToSharePoint -title "My Item Title" -description "My Item Description"

the term is not recognized as the name of a cmdlet function PowerShell

The same PowerShell command was working fine in one of the dev server (Windows 7) machine. But when tried to run in the Windows server 2012 r2 machine, it gave the above error.

One solution I found out was Script interpretation are synchronous in Powershell. So until the interpreter gets to the function, it does not know that the function exists.

I added the function at the bottom of the script after the calling command.

Read some SharePoint PowerShell tutorials:

Then I moved the command to the top of the script and it worked fine without giving any error.

>