0

I have a PowerShell script that I have developed and tested in Visual Studio Code that runs without error. However, when I attempt to run the same script from a PowerShell command line, it fails with several errors, the first one being a complaint about the use of Param on the first line of the script. I have PS 5.1 installed on my machine.

When launched from the command line, here is what I execute:

powershell -ExecutionPolicy Bypass -File Export_PI.ps1 -Project Catalyst-DHT

When I run this script from Visual Studio Code, I substitute a hard-coded value in lieu of the parameter -Project for testing purposes. When I run it from the PS command line, this is the first error I get:

Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program.

I have read elsewhere that this typically occurs when Param() is not the first non-commented line in the script. In my script, it is the first non-commented line:

$ProjectName = Param([String]$Project)

I'm not a PowerShell guru, so this could well be a newbie mistake. I'm hoping someone can help me figure it out. Thanks.

1 Answer 1

1

The line

I have PS 5.1 installed on my machine.

is most likely the issue you are having. VSC uses PowerShell Core (6 <=) and not PowerShell Desktop (6 >)

Also the line

$ProjectName = Param([String]$Project)

is not correct since you don't assign Param to a variable but use it as a statement that assigns variables based on the arguments provided. You will want

Param([String]$ProjectName)

as the first line of your script. Like the error states, Param is not the name of a cmdlet, function, script file, or operable program when not used alone at the top of a script.

Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Thank you. My effort to assign the parameter value to a variable was wrong. It does raise the question: if I were to pass more than one variable, it seems that I would simply have to extract them in the same order they are applied?
you can add more objects to Param for more arguments like Param([type]$arg1, [type]$arg2) etc then access them accordingly with $arg1 and $arg2. See learn.microsoft.com/en-us/powershell/module/… and learn.microsoft.com/en-us/powershell/module/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.