ChatGPT解决这个技术问题 Extra ChatGPT

PowerShell 2.0 and "The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program"

I am running Windows 7 RTM. PowerShell 2.0 is installed by default. I am using the excellent Windows PowerShell ISE to edit my scripts. I have the following script:

Param($p)
Param($d)
echo $p $d

I save the script as SayItAgain.ps1. When I try to run this script from the interactive shell like so:

./SayItAgain -p "Hello"

I receive the following error:

The term 'Param' 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 C:\users\cius\Code\powershell\SayItAgain.ps1:2 char:6
+ Param <<<< ($destination)
    + CategoryInfo          : ObjectNotFound: (Param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Is this a known issue or am I simply using it wrong?

I am wondering if the actual error is on a different line of the script. You say you are calling ./SayItAgain.ps1 but the error says you are calling FindAndCopyFiles.ps1. Is there any more code you can share? Sometimes errors can point you in the wrong direction. The error may be elsewhere.
Ah, I apologize for the inconsistency. I created SayItAgain as a simple example to track down the problem. I copied the wrong error, the one from the original script. I'll explain this in my addendum.

P
Peter Mortensen

If your param($p) is not the first line in your script that can cause the Param error.

Make sure your param($p) is the first line.


This is most likely the problem but first non-commented line would be sufficient.
While the accepted answer by Jon Ingle would have solved his problem. I feel this is most likely the problem about 'params' other programmers would have faced and exact solution being provided. Thanks @David.
if only the error message could have mentioned this!
I'm still surprised "must be first line of the file" is a go-to solution that persists through new languages. Since there can only be a single "first line", it just seems like a hack.
Beware that azure dev-ops inserts other lines in front of inline powershell scripts. See other answers.
J
JayTurnr

I've solved the problem. I've corrected the description of the problem to make it accurate.

The source of the problem is that I was incorrectly using the Param keyword multiple times. The correct usage is to declare multiple parameters within a single Param declaration like the following:

Param($p, $d)

This usage is explained in the Windows PowerShell Help article "about_Functions".


P
Paul Rowland

Running this script,

cls

param([string]$Url, [string]$Template="CMSPUBLISHING#0")

Write-Host "Url: $Url"  

I got the same error

The term 'param' 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.

When I commented out the cls at the top it worked,

#cls 

param([string]$Url, [string]$Template="CMSPUBLISHING#0")

Write-Host "Url: $Url"

There can't be anything other than whitespace or comments before the use of the param keyword, which is why you got the error.
V
Vladyslav Hrehul

Also had this "kind" of problem, but I couldn`t run any of my ps1 scripts. Solution: Change encoding from UTF-8 (or which you have) to ANSI. Perhaps it will help someone too.


m
martinseal1987

I had this issue when trying to use a powershell inline script in an azure dev-ops pipeline, turns out this is an issue so will be using a script from a file instead


True. It's not possible to pass parameters to an inline script in azure dev-ops. Here are links to the issue : github.com/MicrosoftDocs/azure-devops-docs/issues/9578, github.com/microsoft/azure-pipelines-tasks/pull/14962