Aylarja
Aylarja

Reputation: 27

PowerShell: Running Script from Visual Studio Code Works, Running from PS Command Line Fails

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.

Upvotes: 0

Views: 955

Answers (1)

Nico Nekoru
Nico Nekoru

Reputation: 3102

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.

Upvotes: 1

Related Questions