How to Pass Arguments to a PowerShell Script

How to Pass Arguments to a PowerShell Script

Passing arguments to a PowerShell script lets you provide input values when running it, making a single script flexible for different inputs without editing it. Windows 11’s PowerShell supports parameters that receive arguments cleanly.

The Command

param($Name); Write-Output "Hello, $Name"

What It Does

Placing `param($Name)` at the top of a script defines a parameter named Name that receives an argument. When you run the script with a value, that value fills $Name, so the script can use it. This example greets YYGACOR Login whatever name you pass, letting one script produce different output based on the input provided.

When You’d Use This

This makes a script reusable for different inputs without editing it each time, such as a script that processes whatever file or name you pass when running it. Parameters turn a fixed script into a flexible tool, which is essential for scripts you run repeatedly with varying inputs, letting one script serve many situations through the arguments you supply.

Useful Variations

You run such a script with `.\script.ps1 -Name “Alice”`, supplying the argument by parameter name. Scripts can define several parameters, and you can set default values, such as `param($Name = “World”)`, used when no argument is given. Parameters can also be typed and marked mandatory for more robust scripts.

If It Doesn’t Work

If PowerShell does not recognize your parameter, ensure the `param` block is the very first executable statement in the script, before other commands. Pass arguments by name, as with `-Name “value”`, which is clearer and order-independent than relying on position. Setting default values with `param($Name = “World”)` avoids errors when no argument is given, making the script more robust for varied use.

Good to Know

The `param` block must be the first executable statement in the script, before other commands, or PowerShell will not recognize it. Naming parameters and passing them by name, as with `-Name`, makes script calls clear and order-independent, which is more reliable than depending on the position of arguments.

Putting It Together

Once you have run it once or twice, this becomes second nature. As part of working with output and building simple automation, this command is a building block you will reuse constantly. As you combine it with the other scripting basics here, small one-off commands grow into reusable scripts that save real time on repetitive work. Like anything in the terminal, the real value comes from trying it on your own system and adapting the variations above to what you actually need, so it is worth experimenting with in a safe, low-stakes situation before relying on it in a script or during troubleshooting. Keeping a note of the commands you find most useful, along with the variations that fit your workflow, turns scattered one-off tricks into a personal reference you can draw on whenever a similar task comes up again.

By john

Leave a Reply

Your email address will not be published. Required fields are marked *