How do I pass parameters to PowerShell scripts? | PowerShell FAQ (2024)

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value. If the parameter is a "switch" then specifying it's name sets it to True, so you should not provide a value. Passing multiple values to the same parameter requires commas (and optionally, the @(...) array specifier).

Accepting parameters in a PowerShell script (or function) is done by adding a param block at the top with a comma-separated list of parameter names:

function Test-Script { param($Name,$Age) "$Name is roughly $Age years old"}Test-Script "The Doctor" "4.5 billion"Test-Script -Name Mother -Age 29

Last updated

How do I pass parameters to PowerShell scripts? | PowerShell FAQ (2024)

FAQs

How do I pass parameters between PowerShell scripts? ›

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How to do parameters in PowerShell? ›

The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.

How to pass multiple values of a parameter in PowerShell script? ›

To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3".

How do you pass parameters from function to function in PowerShell? ›

One way to use PowerShell function parameters in a script is via a parameter name -- this method is called named parameters. When calling a script or function via named parameters, use the entire name of the parameter. For example, perhaps the example param block above is stored in a PowerShell script called foo. ps1.

How to pass parameter to script? ›

To pass an argument to your Bash script, your just need to write it after the name of your script:
  1. ./script.sh my_argument.
  2. #!/usr/bin/env bash. ...
  3. ./script.sh. ...
  4. ./fruit.sh apple pear orange. ...
  5. #!/usr/bin/env bash. ...
  6. ./fruit.sh apple pear orange. ...
  7. © Wellcome Genome Campus Advanced Courses and Scientific Conferences.

How do you pass parameters to a script file? ›

The first method for passing arguments from a file to a Bash script is to use redirection. In particular, the input redirection operator (<) redirects the contents of the file to the standard input of the Bash script. This way, we can extract the arguments from a file using the read command.

How to use parameters in shell script? ›

A bash shell script have parameters. These parameters start from $1 to $9. When we pass arguments into the command line interface, a positional parameter is assigned to these arguments through the shell.

What are common parameters in PowerShell script? ›

General Common Parameters
  • Debug (alias: db) Data type: SwitchParameter. ...
  • ErrorAction (alias: ea) Data type: Enumeration. ...
  • ErrorVariable (alias: ev) Data type: String. ...
  • InformationAction (alias: infa) ...
  • InformationVariable (alias: iv) ...
  • OutBuffer (alias: ob) ...
  • OutVariable (alias: ov) ...
  • PipelineVariable (alias: pv)
Oct 4, 2023

What is an example of a parameter function in PowerShell? ›

Let's take a look at an example: function Greet-User { param ( [Parameter(Mandatory=$true)] [string]$Name ) Write-Output "Hello, $Name!" } In this version of Greet-User , the $Name parameter is mandatory.

How many parameters we can pass to shell script? ›

The shell allows a command line to contain at least 128 arguments; however, a shell program is restricted to referencing only nine positional parameters, $1 through $9, at a given time.

What is the parameter function in PowerShell? ›

PowerShell uses the parameter value order to associate each parameter value with a parameter in the function. When you use positional parameters, type one or more values after the function name. Positional parameter values are assigned to the $args array variable.

How to call a PowerShell script from a batch file with parameters? ›

Your batch file can be simpler: @Echo Off Echo Starting Script for MYCompany... PowerShell.exe -ExecutionPolicy Bypass -File ""%~dp0\My-Custom. ps1" -UserExcluded <input for parameter, if needed>" Echo Exiting Script...

How to pass parameters from command line? ›

Enclose the variable=value clause in parentheses. If the parameter contains any characters treated specially by your operating system (such as parentheses in Windows NT), enclose the entire parameter within double quotes and escape any embedded double quotes to pass them through the operating system.

What is the default way of passing parameters to a function? ›

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function).

What is passing parameters to functions? ›

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.

How to call one script from another script in PowerShell? ›

You can write an entire library of functions and save each to a separate file, then use dot-sourcing to call them from a script. You could even go a step further and dot-source the functions into your PowerShell profile to make them available every time you launch PowerShell.

How do I pass user input in PowerShell? ›

The easiest way to get input from the user in PowerShell is via the Read-Host cmdlet. This cmdlet prompts the user for input and waits for the user to type a response. It can be used to collect various types of input, such as strings, numbers, and secure strings.

How do I pass a default parameter in PowerShell? ›

Default parameter sets

When multiple parameter sets are defined, the DefaultParameterSetName keyword of the CmdletBinding attribute specifies the default parameter set. PowerShell uses the default parameter set when it can't determine the parameter set to use based on the information provided to the command.

What is splatting in PowerShell? ›

Splatting is a method of passing a collection of parameter values to a command as a unit. PowerShell associates each value in the collection with a command parameter.

Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5792

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.