75

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
user3505712
  • 895
  • 1
  • 11
  • 20
  • The screenshot is not working – Timo Oct 24 '20 at 06:27
  • Related post - [Error: The term './RunCommandLine.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program](https://stackoverflow.com/q/32507107/465053) – RBT Mar 17 '21 at 06:36

3 Answers3

78

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).

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
3

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!"
JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
0

Yet another way this error message can occur...

If PowerShell is open in a directory other than the target file, e.g.:

If someScript.ps1 is located here: C:\SlowLearner\some_missing_path\someScript.ps1, then C:\SlowLearner>. ./someScript.ps1 wont work.

In that case, navigate to the path: cd some_missing_path then this would work:

C:\SlowLearner\some_missing_path>. ./someScript.ps1

SlowLearner
  • 3,086
  • 24
  • 54
  • Well, yes, because in a path, `.` means "the current directory". If you are not trying to use something in the current directory, then `.` would be an invalid path. You don't need to switch directories so that `.` works, just specify an actual valid path: `. C:\SlowLearner\some_missing_path\someScript.ps1` – AndrewF Feb 14 '22 at 01:37
  • @AndrewF - indeed. The intent was merely to shine a light on how such an error might be produced. – SlowLearner Feb 15 '22 at 05:36