Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell The term is not recognized as cmdlet function script file or operable program

I am implementing a script in powershell and getting the below error. The sceen shot is there exactly what I entered and the resulting error.

enter image description here

At this path there is file Get-NetworkStatistics.ps1 which I got from Technet Gallery. I am following the steps from it, though there are errors.

like image 493
user3505712 Avatar asked Jun 11 '14 06:06

user3505712


People also ask

How do you fix is not recognized as the name of a cmdlet?

If that module is missing, corrupt, or got moved, it throws up the error, “the term is not recognized as the name of a cmdlet.” You can use “get-module” in PowerShell to see if the module is present and correct. It will show you what modules are loaded, and you can add or repair them depending on your needs.

Why is PowerShell not recognizing commands?

Cause. This is caused by the user (or system) PATH environment variable not containing the directory where the PowerShell executable resides. It's usually located at C:\Windows\System32\WindowsPowerShell\v1.


2 Answers

You first have to 'dot' source the script, so for you :

. .\Get-NetworkStatistics.ps1 

The first 'dot' asks PowerShell to load the script file into your PowerShell environment, not to start it. You should also use set-ExecutionPolicy Unrestricted or set-ExecutionPolicy AllSigned see(the Execution Policy instructions).

like image 78
JPBlanc Avatar answered Sep 18 '22 06:09

JPBlanc


For the benefit of searchers, there is another way you can produce this error message - by missing the $ off the script block name when calling it.

e.g. I had a script block like so:

$qa = {     param($question, $answer)     Write-Host "Question = $question, Answer = $answer" } 

I tried calling it using:

&qa -question "Do you like powershell?" -answer "Yes!" 

But that errored. The correct way was:

&$qa -question "Do you like powershell?" -answer "Yes!" 
like image 24
JsAndDotNet Avatar answered Sep 18 '22 06:09

JsAndDotNet