Share via


How to pass parameters to a PowerShell ISE script?

Question

Tuesday, November 26, 2019 3:19 PM

Hello,

   I am trying to figure out on how to pass parameters to a PowerShell ISE script.

I took a snap shot of my simple script and tried to find out how to pass parameter to it. I could not do it from the Run button on top since there was no place for placing the parameters. I would like to seek your expertise if you happen to know a way on how to do it.

Many Thanks!!

Student at Georgia Tech

All replies (20)

Tuesday, November 26, 2019 3:39 PM ✅Answered

You have to define a "Param" statement in your script and the parameters wil be prompted.

Apparently your documentation or training materials are out of date.  Update your docs and system to WMF 5.1.

\(ツ)_/


Tuesday, November 26, 2019 5:34 PM ✅Answered

Please stop posting images of code. They are not helpful,

See: An image of code is not helpful

You really need to start with a modern book on PowerShell,  It will change your view and help you understand how to get information on commands and usage from PowerShell help and utility commands.  You are missing these fundamental steps in learning.  Yu also don't seem to have any understanding of what PowerShell is.  It is called a "scripting environment" and not a "scripting language".  Once you understand what this means things will go more smoothly and information will be easily available.

Start here: https://www.sapien.com/books_training/Windows-PowerShell-4

It is a free copy of a book on PowerShell that is well reviewed and written by senior admin/techs who also teach PowerShell.  THre are othe grewaat books but this one has been posted for free.  I recommend reading through the introduction and first few chapters t get reoriented to what PS is and how it is intended to work.  I can also recommend purchasing a copy of the book from Amazon while they last as it is much easier to look things up in the paper version.  I usually get paper and read chapters at lunch or just while relaxing with an eye towards completely understanding each concept.  It is an invaluable exercise and an Georgia Tech student should become adept at consuming book level learning.  Practice makes this painless and fun.  I became efficient with this when I first started working in computer engineering.  It has taken me around the world and into dozens of interesting and challenging projects.

\(ツ)_/


Tuesday, November 26, 2019 3:44 PM

Thank you so much for all of advice and pointers.

Student at Georgia Tech


Tuesday, November 26, 2019 3:45 PM

Thank you so much for all of the helps!

Student at Georgia Tech


Tuesday, November 26, 2019 3:48 PM

You can also use VS Code (instead of the ISE) and modify the launch.json file to provide those values without being prompted for them each time you run the script.

Here's a nice explanation: https://github.com/PowerShell/vscode-powershell/tree/master/examples#passing-arguments-to-the-script

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


Tuesday, November 26, 2019 3:51 PM

You cannot set "$args" variable as it is a protected variable in PowerShell. It cannot be created or assigned.  It is automatically created or visible when passing arguments to a script or function.

\(ツ)_/


Tuesday, November 26, 2019 4:14 PM

Sorry, I may have done something wrong since I have updated my Power Shell to a new version but it still does not give me a prompt to enter my parameter. I am still trying to find out what wrong with my simple code. Thank you so much for all of the helps.

Attached is my snap shot

Student at Georgia Tech


Tuesday, November 26, 2019 4:21 PM

And yet . . .

The tiny script:

param( $t )
write-host $t

The file launch.json (note the 'args' entry):

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "PowerShell",
            "request": "launch",
            "name": "PowerShell Launch Current File",
            "script": "${file}",
            "args": ["-t 'Hello, World!'"],
            "cwd": "${file}"
        },        
        {
            "name": "PowerShell: Interactive Session",
            "type": "PowerShell",
            "request": "launch",
            "cwd": ""
        }
    ]
}

And the result of "Start Debugging" (i.e. pressing "F5"):

PS C:\junk> c:\Junk\Untitled-1.ps1


Hello, World!
PS C:\Junk> 

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


Tuesday, November 26, 2019 4:29 PM

Rich - has nothing to do with PS args.

\(ツ)_/


Tuesday, November 26, 2019 4:30 PM

Run this in ISE and you will be prompted:

Param(
[parameter(Mandatory)]$test
)

function ftest{
Param(
    [parameter(Mandatory)]$MyTest
    )
}

ftest

\(ツ)_/


Tuesday, November 26, 2019 4:30 PM

Hello Rich,

    Thank you so much for all of the helps. I am having a Visual Studio Community version , and I would like to add Power Shell version into it. I found an article on Google which suggested that I went to the Visual Studio Installer , selected the Individual Component and then selected Power Shell. I followed but could not locate Power Shell on the Individual Component. I wonder if I have missed some important steps that may not have been from the Google article.

Attached is my snap shot of my Visual Studio Community

 

Student at Georgia Tech


Tuesday, November 26, 2019 4:35 PM

Hello Rich,

    Thank you so much for all of the helps. I am having a Visual Studio Community version , and I would like to add Power Shell version into it. I found an article on Google which suggested that I went to the Visual Studio Installer , selected the Individual Component and then selected Power Shell. I followed but could not locate Power Shell on the Individual Component. I wonder if I have missed some important steps that may not have been from the Google article.

Attached is my snap shot of my Visual Studio Community

 

Student at Georgia Tech

Please do not add new questions to a topic.  It will confuse others and Rich's suggestion will not solve your issue with ISE or with PowerShell arguments.

Stick to the original question and try to understand what my code is showing you.

For issue with installing and using VSC post in the VSC forum.  VSC is designed for advanced users and programmers.  Y0u are still learning the basics of PowerShell and need to stay simple until you master the basics.  VSCode will only add complexity as it will take a non-programmer a few days to understand and get working.

\(ツ)_/


Tuesday, November 26, 2019 5:05 PM

I apologize for the unrelated question on the Visual Code. Thank you so much for all of the helps.

The given Power Shell example works very well , and I am able to get the prompt.

May I ask if there is a possibility to declare a Mandatory parameter as type of String or Int value such as

   Param([parameter(Mandatory String)]$test) 

I suspect that it may not be working because I got a run time error.

. I am still finding out on how to add a String or Int type in the parameter if I could.

I also have another question related to adding more than one parameter to the script. It works great but I am still trying to fix the double prompts that I have on one of the test. It may be due to the fact that something there is an error on my script and I am trying to fix it.

Attached is my snap shot of the simple script 

 

Student at Georgia Tech


Tuesday, November 26, 2019 6:26 PM

Thank you.

Student at Georgia Tech


Tuesday, November 26, 2019 7:17 PM

But it has to do with the question in the OP's post about how to pass parameters values to a script while debugging -- just not with the Powershell ISE.

I didn't mention **$**args, nor did the link I posted.

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


Tuesday, November 26, 2019 7:20 PM

You don't need VS Studio, at least not unless you're developing modules or programs that require it. Use Visual Studio Code.

https://code.visualstudio.com/

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


Tuesday, November 26, 2019 7:52 PM

If test.ps1 was (notice "args" with the "s")

$arg1 = $args[0]
$arg2 = $args[1]

write-host "arg one is " + $arg1
write-host "arg two is " + $arg2

At the prompt you could run:

.\test.ps1 whatever1 whatever2

And get the output:

arg one is  + whatever1
arg two is  + whatever2


Tuesday, November 26, 2019 8:20 PM

But it has to do with the question in the OP's post about how to pass parameters values to a script while debugging -- just not with the Powershell ISE.

I didn't mention **$**args, nor did the link I posted.

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

The OP was trying to use ARGS and that cannot be set in a profile or configuration file.  I also suggest that new users should not use VS or VSCode if they are not somewhat experienced with coding with an IDE>  The ISE is comfortable but limited and has behaviors that can cause confusion.  Using the PS console is best for understanding PowerShell and the process of coding.  The only better tools I have found are PrimalScript and PowerShell Studio but both are IDEs and also can confuse new users with no programming experience.

Visual Studio PowerShell support is thin but easier to set up and use than VSCode.  VSCode is more flexible and powerful but takes some time for a fist time coder.

The best path is always to use a book to get started and stick to it until you understand the basics of the PowerShell system completely.  After that any tool will do once it is learned.  Again reading the documentation for the tool is the first place to start.

I find that most kids today don't like to read and are very challenged by technical documentation.  That needs to change or we will end stuck with robots doing everything for us. 

\(ツ)_/


Tuesday, November 26, 2019 9:01 PM

If test.ps1 was (notice "args" with the "s")

$arg1 = $args[0]
$arg2 = $args[1]

write-host "arg one is " + $arg1
write-host "arg two is " + $arg2

At the prompt you could run:

.\test.ps1 whatever1 whatever2

And get the output:

arg one is  + whatever1
arg two is  + whatever2

Not the issue asked about.  The OP is asking how to get the script to prompt for the value in the ISE.   It won't with this.  This just demonstrates a variable wasting way to use the "args" PowerShell protected variable which is empty if no arguments are passed. 

Run this in ISE -

Write-Host $args[0]

There will be no prompt.

\(ツ)_/


Wednesday, November 27, 2019 3:16 PM

Running the script from the prompt is close to what he wants, and is almost the same thing as hitting the "play" button.  Actually, clicking play is like sourcing the script.