90

See Title.

I specified needed parameters in the head of a script:

param ($G_ARCHIVE = $(throw "Need file to upload!"),
       $G_LOGFILE = $(throw "Need logfile!"))

When I want to debug the script with Powershell ISE: how can I fill these parameters?

6 Answers 6

92

Use the command pane. Open the script file in the ISE editor, set the breakpoints (F9). Then in the command pane type a command invoking this script with required parameters. I do not think there is another (built-in) way of doing this in ISE.

2
  • 9
    This approach did not work for me since my filepath had spaces in it, the solution was to use the "ampersand-function" in the following way: PS C:\Windows> &("c:\my folder\script.ps1") -myArg 123 -anotherArg abc
    – Emil G
    Commented Sep 8, 2014 at 12:33
  • 5
    You can actually use the relative path: .\script.ps1 Commented Sep 25, 2014 at 6:33
37
  1. Open the script (myscript.ps1) in Windows Powershell ISE
  2. Press F9 at the variable you want to inspect (debug). For instance 2nd line in the sample below where the $outputText variable is being assigned
  3. In the shell window provide the relative path of the script along with the param value. For instance: .\myscript.ps1 "my value"
  4. Hit enter (you don't need to hit F5)
  5. You'll be able to see the debugging breakpoints in highlighted with yellow color. Place your cursor to the desired variable to inspect the current value.

A sample showing PowerShell debugging with ISE and command parameter

3
  • 2
    how is this answer different from the accepted one?
    – eckes
    Commented Sep 25, 2014 at 7:55
  • 10
    Thought explaining more details with illustration will be helpful for others. For instance providing the relative (or absolute) location is required, which was not provided in the first place. Commented Sep 25, 2014 at 9:16
  • 2
    This answer should be the accepted answer because it actually explains what to do and shows the two panes. I could use this answer, the other one I didn't understand it well enough to know what to do.
    – PatS
    Commented Oct 15, 2023 at 3:29
19

There is another way. You can use the $PSDefaultParameterValues automatic variable, which exists (since v3) to provide new default arguments to cmdlets and advanced functions (doesn't work with normal functions). However, it does work for scripts, even when debugging in ISE. You have to declare [CmdletBinding()] or [Parameter()] like you would for an advanced function.

So for your example,

[CmdletBinding()]
param ($G_ARCHIVE = $(throw "Need file to upload!"),
$G_LOGFILE = $(throw "Need logfile!"))

you would execute something like this on the ISE Prompt:

$PSDefaultParameterValues.add("ExampleScript.ps1:G_ARCHIVE","File-to-upload.txt")
$PSDefaultParameterValues.add("ExampleScript.ps1:G_LOGFILE","Example.log")

You could also set the parameter value to a script block which will auto-execute at run-time:

$PSDefaultParameterValues["ExampleScript.ps1:G_LOGFILE"]={
  "Example-{0:yyMMddHHmm}.log" -f [datetime]::Now
}

The variable is a hashtable and all the standard syntax applies, except the key must have the name of the script (or advanced function or cmdlet) followed by a colon then the parameter name. You can set defaults for multiple scripts or commands, and multiple parameters for each (each parameter is a new table entry).

By doing it this way, you can just hit F5 to run your script like normal. The parameters will be taken from the variable, so you don't have to type anything in.

Other use cases for $PSDefaultParameterValues might be customizations, like have the Get-History get only the last 10 entries, unless you specify the -Count parameter in the command. Because entries only persist for the current session, you would want to add customizations to your profile. You can read more by typing Get-Help about_Parameters_Default_Values at the prompt or view the same information on TechNet.

4
  • Another script block example. You could have your script automatically use the newest zip file. $PsDefaultParameterValues=["ExampleScript.ps1:G_ARCHIVE"]={(ls *.zip | sort lastwritetime | select -last 1).fullname} This kind of thing is super useful when you are constantly creating new versions of some external data set to test and debug your script with. Commented Apr 10, 2016 at 18:20
  • $PSDefaultParameterValues.Item("KEY") = "VALUE" is another alternative
    – RichardOD
    Commented Dec 6, 2016 at 16:51
  • 1
    It only works in scripts when parameters are declared with a [Parameter()] decorator, which is not the case in @eckes example. Commented Apr 27, 2018 at 17:41
  • You're right. I mentioned it needed to be an advanced function, but not that you had to do the same with a script. Commented Apr 30, 2018 at 16:38
2

There is a much simpler way to set needed Parameters in ISE:

Before pressing F5 in ISE, set the Parameter you need. I usually comment the Parameter I need, example: # $G_ARCHIVE = "C:\Temp\TestFile_001.txt"

I select everything after "#" and press F8. Next time I debug the script with F5, the Parameter is set to the value I am testing with, no need to pass the Parameters through the command line.

0
1

At least in Powershell 5.1 ISE when you press F5 to run a parameterized script, you will be asked to enter values for the parameters one by one.

When using the $PSDefaultParameterValues to populate the variables, you can reference the loaded script through the $psISE variable like

$PSDefaultParameterValues.add("$($psISE.CurrentFile.DisplayName):G_ARCHIVE","test")
0

I had this problem today. After messing around I discovered that the lower panel was running in Debug E.g. Prompt '[Dbg]: PS'
I stopped the debugging and re-issued my script with paramaeters. Example: ./myScript.ps1 -ForceBuild $true

My results were that the script started and the breakpoints fired and allowed me to debug as expected.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.