How to Run Command Line Argument Using Powershell Script?

9 minutes read

To run a command line argument using a PowerShell script, you can use the syntax:

1
2
3
4
5
param(
    [string]$argument
)

Write-Host "Argument passed: $argument"


In this script, the param keyword is used to define a parameter named $argument which will hold the value of the command line argument. The Write-Host cmdlet is used to display the value of the argument passed to the script.


To run the PowerShell script with a command line argument, you need to open a PowerShell window and navigate to the directory where the script is saved. Then, you can run the script with the command line argument like this:

1
.\script.ps1 -argument "value"


Replace "script.ps1" with the name of your script file and "value" with the argument you want to pass to the script. When you run the script, it will display the argument passed as an output.

Best PowerShell Books to Read in December 2024

1
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 5 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

2
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 4.9 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

3
Scripting: Automation with Bash, PowerShell, and Python

Rating is 4.8 out of 5

Scripting: Automation with Bash, PowerShell, and Python

4
Learn PowerShell Scripting in a Month of Lunches

Rating is 4.7 out of 5

Learn PowerShell Scripting in a Month of Lunches

5
Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

Rating is 4.6 out of 5

Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

6
Practical Automation with PowerShell: Effective scripting from the console to the cloud

Rating is 4.5 out of 5

Practical Automation with PowerShell: Effective scripting from the console to the cloud

7
Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Rating is 4.4 out of 5

Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

8
PowerShell for Sysadmins: Workflow Automation Made Easy

Rating is 4.3 out of 5

PowerShell for Sysadmins: Workflow Automation Made Easy

  • Book - powershell for sysadmins: workflow automation made easy
9
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.2 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


What is a command line argument in Powershell?

A command line argument in Powershell is an input given to the Powershell script or command when it is executed from the command line interface. These arguments provide additional information or parameters to the script or command, allowing users to customize its behavior or output. Command line arguments are typically passed as strings separated by spaces after the script or command name.


What is the procedure for accessing command line arguments in Powershell scripts?

In PowerShell scripts, you can access command line arguments using the automatic variable $args or by specifying named parameters in the param block at the beginning of the script.


Here is how you can access command line arguments using $args:

1
2
3
foreach ($arg in $args) {
    Write-Host "Argument: $arg"
}


Alternatively, you can define named parameters in the param block at the beginning of the script and access them by name:

1
2
3
4
5
6
7
param (
    [string]$param1,
    [int]$param2
)

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


When running the PowerShell script, you can pass command line arguments like this:

1
2
.\script.ps1 arg1 arg2    (for accessing $args)
.\script.ps1 -param1 value1 -param2 value2    (for accessing named parameters)



How to process multiple command line arguments in Powershell scripts?

To process multiple command line arguments in a Powershell script, you can use the $args array to access all the arguments passed to the script. Here is an example of how you can process multiple command line arguments in a Powershell script:

1
2
3
4
5
6
7
8
9
# Save input arguments into variables
$arg1 = $args[0]
$arg2 = $args[1]
$arg3 = $args[2]

# Process the arguments
Write-Host "Argument 1: $arg1"
Write-Host "Argument 2: $arg2"
Write-Host "Argument 3: $arg3"


In this example, the script accesses the first three arguments passed to it using the $args array and assigns them to variables $arg1, $arg2, and $arg3. These variables are then used to perform any necessary processing within the script.


To run this script and pass multiple arguments to it, you can use the following command in the Powershell console:

1
.\script.ps1 arg1 arg2 arg3


Replace script.ps1 with the name of your Powershell script file and arg1, arg2, and arg3 with the actual arguments you want to pass to the script. The script will then process these arguments accordingly.


What is the purpose of command line arguments in Powershell scripts?

Command line arguments in Powershell scripts are used to pass parameters or input values to the script when it is executed. This allows for more flexibility and customization in the script as the values can be varied each time the script is run without needing to modify the code. Command line arguments can be used to specify file paths, configuration settings, or any other information that the script needs to perform its tasks.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To execute a PowerShell script from Excel, you can use the "Shell" function in VBA (Visual Basic for Applications). First, you need to create a macro in Excel that will run the PowerShell script. Within the macro, use the Shell function to launch Power...
To run a PowerShell script in Maven, you can use the Maven Exec Plugin. This plugin allows you to execute command-line programs, including PowerShell scripts, from within your Maven project.To use the Maven Exec Plugin, you need to add it to your project's...
You can execute a multi-line PowerShell script using Java by first creating a ProcessBuilder object and setting the command to "powershell.exe". Then, you can pass the script as a parameter to the ProcessBuilder object using the command line arguments....