Skip to main content
infervour.com

Back to all posts

How to Pass Parameters to Powershell Script From Batch File?

Published on
4 min read
How to Pass Parameters to Powershell Script From Batch File? image

Best PowerShell Scripting Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
4 The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition

The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition

BUY & SAVE
$29.99
The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition
5 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$34.99
Learn PowerShell Scripting in a Month of Lunches
6 Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black

Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black

  • VERSATILE FOR VARIOUS TASKS: PERFECT FOR DOORS, FLOORING, TILING, AND MORE!

  • PRECISION ADJUSTABLE OFFSET: ACHIEVE A PERFECT FIT WITH EASY 0.04 TO 1.57 ADJUSTMENTS.

  • ULTRA-THIN GUIDE PLATE: ACCESS TIGHT SPACES EFFORTLESSLY WITH A 0.02 PLATE.

BUY & SAVE
$31.99
Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black
7 Milescraft 8407 ScribeTec - Scribing and Compass Tool

Milescraft 8407 ScribeTec - Scribing and Compass Tool

  • ARTICULATING HEAD FOR TACKLING COMPLEX ANGLES EFFORTLESSLY.
  • PRECISION POINT LOCKS FOR PERFECT RADIUS AND CLEAN LINES.
  • VERSATILE GRIP FITS PENCILS AND MARKERS FOR ALL YOUR NEEDS.
BUY & SAVE
$10.49 $10.99
Save 5%
Milescraft 8407 ScribeTec - Scribing and Compass Tool
8 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
9 FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool

FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool

  • ADJUSTABLE GRIP ENSURES COMFORT FOR ALL STANDARD PENCILS.
  • CONSISTENT SCRIBE OFFSET FOR PERFECT PARALLEL LINES EVERY TIME.
  • DURABLE POLYMER DESIGN FOR LONG-LASTING PERFORMANCE AND RELIABILITY.
BUY & SAVE
$17.99
FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool
+
ONE MORE?

To pass parameters to a PowerShell script from a batch file, you can use the following syntax:

  1. In the batch file, call PowerShell with the script file and pass the parameters using the -File flag. For example:

powershell -File "C:\path\to\script.ps1" -Parameter1 "value1" -Parameter2 "value2"

  1. In your PowerShell script, you can access the passed parameters using the $args or named parameters. For example:

param( [string]$Parameter1, [string]$Parameter2 )

Write-Host "Parameter 1: $Parameter1" Write-Host "Parameter 2: $Parameter2"

  1. Make sure to properly handle any user inputs to prevent any security vulnerabilities or unexpected behavior.

By following these steps, you can easily pass parameters to a PowerShell script from a batch file.

How to use splatting to pass parameters to a PowerShell script?

Splatting is a technique in PowerShell that allows you to pass a collection of parameter values to a command or script as a hashtable. This can make your code more readable and maintainable, especially when dealing with a large number of parameters.

To use splatting to pass parameters to a PowerShell script, you can follow these steps:

  1. Define a hashtable that contains the parameter names and values that you want to pass to the script. The keys of the hashtable should be the parameter names, and the values should be the parameter values.

$params = @{ Param1 = "Value1" Param2 = "Value2" Param3 = "Value3" }

  1. Use the splatting operator (@) to pass the hashtable to the script. You can do this by using the & operator to invoke the script and passing the hashtable as an argument.

& "C:\path\to\script.ps1" @params

  1. In the PowerShell script, access the parameters using the automatic variable $args, which represents an array of values that were passed to the script.

param( [string]$Param1, [string]$Param2, [string]$Param3 )

Write-Host "Param1: $Param1" Write-Host "Param2: $Param2" Write-Host "Param3: $Param3"

By using splatting, you can make your PowerShell scripts more flexible and easier to maintain by passing parameters as a hashtable rather than individual values.

How to pass parameters to a PowerShell script?

To pass parameters to a PowerShell script, you can use the $args array or named parameters. Here's how you can do it:

  1. Using the $args array: You can access the parameters passed to a script using the $args array. Each parameter will be stored in the array indexes, starting with 0, 1, 2, and so on.

Here's an example of how you can pass parameters using the $args array in a PowerShell script:

param( $param1, $param2 )

Write-Host "Parameter 1: $param1" Write-Host "Parameter 2: $param2"

To run the script and pass parameters:

.\script.ps1 value1 value2

  1. Using named parameters: You can also define named parameters in your script by using the param keyword. This allows you to specify the parameters explicitly when calling the script.

Here's an example of how you can pass named parameters in a PowerShell script:

param( [string]$param1, [string]$param2 )

Write-Host "Parameter 1: $param1" Write-Host "Parameter 2: $param2"

To run the script and pass named parameters:

.\script.ps1 -param1 value1 -param2 value2

Using named parameters is more explicit and can make your script more readable and easier to understand.

How to pass arrays or collections as parameters to a PowerShell script?

To pass arrays or collections as parameters to a PowerShell script, you can use the [array] type accelerator or the [System.Collections.Generic.List[object]] type.

Here's an example of how to define a script that takes an array as a parameter:

param( [array]$myArray )

foreach ($item in $myArray) { Write-Output $item }

You can then call this script and pass an array as a parameter like this:

$myArray = @("item1", "item2", "item3") .\MyScript.ps1 -myArray $myArray

Alternatively, you can also use a generic collection like a List as a parameter in your script:

param( [System.Collections.Generic.List[object]]$myList )

foreach ($item in $myList) { Write-Output $item }

And then pass a List as a parameter like this:

$myList = New-Object System.Collections.Generic.List[object] $myList.Add("item1") $myList.Add("item2") $myList.Add("item3") .\MyScript.ps1 -myList $myList

Using these methods, you can pass arrays or collections as parameters to a PowerShell script.

How to encode special characters in parameters passed to a PowerShell script?

Special characters in parameters passed to a PowerShell script can be encoded using the backtick (`) character or by enclosing the parameter value in double quotes.

For example, if you need to pass a parameter value that contains a space or a special character such as an ampersand (&) or a dollar sign ($), you can encode it using the backtick character like this:

.\yourScript.ps1 -paramName "value` with` special` characters"

Alternatively, you can enclose the parameter value in double quotes like this:

.\yourScript.ps1 -paramName "value with special characters"

This will ensure that the special characters are correctly passed to the script and processed by PowerShell.