How do I pass 2 parameters into a powershell script?

I have two parameters I want to pass into a poweshell script

i.e. PS c:\>.\test.ps1 value 1,value 2
LVL 2
SiHodgy007Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

I would do it like this (with the script below).

Then you would run the command like this:

./Zip.ps1 -File "SomeFile.txt" -Archive "archive.7z"

Using the named parameters like this means it doesn't matter which order you enter the details.

Is that the kind of thing you had in mind?

Chris
# Script Zip.ps1
Param(
  $Archive = $(Throw "Archive Name is required"),
  $File = $(Throw "File to add to Archive is required"))
 
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
 
Invoke-Expression "$7Zip a -mx=9 $Archive $File"

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

Without the comma:

test.ps1 value1 value2

Then you can deal with the parameters as you see fit within the script itself.

Chris
0
 
SiHodgy007Author Commented:
how do I call these values in the script say if i just want to echo them?
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Chris DentPowerShell DeveloperCommented:

You have quite a bit of choice about how you deal with parameters.

You can used named or positional, using named is nice if you want someone to use the script, positional is fine if it's nothing more than an internal command.

A few little samples of both here. Each just echoes the parameter using Write-Host.

Chris
# Command to call the scripts below
 
# Named Paramaters
./Test1.ps1 -Param1 "Bob"
./Test1.ps1 -Param1 "Bob" -Param2 "Tim"
./Test1.ps1 -Param2 "Alan"
./Test1.ps1 -Param2 "Beth" -Param3 "Rachel"
 
# Positional Parameters
./Test2.ps1 "Bob" "Tim"
./Test2.ps1 "Bob"
 
# Test1.ps1
Param(
  $Param1 = "Default Value",
  $Param2 = $(Throw "This parameter is required"),
  $Param3)
 
Write-Host "Param1 has a default value. Current value is: $Param1"
Write-Host "Param2 is required. Current value is: $Param2"
Write-Host "Param3 is optional. Current value is: $Param3"
 
# Test2.ps1
Write-Host "Total Arguments: $($Args.Length)"
ForEach ($Arg in $Args) {
  $i++; Write-Host "$i : $Arg"
}

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

You can use Functions to test these instead of saving things off as scripts. Might just be easier to play around with param there, it works in exactly the same way as with scripts.

They'll copy and paste into the shell and live as long as you keep that shell open.

Chris
# Functions
 
Function Test1 {
  Param(
    $Param1 = "Default Value",
    $Param2 = $(Throw "This parameter is required"),
    $Param3)
 
  Write-Host "Param1 has a default value. Current value is: $Param1"
  Write-Host "Param2 is required. Current value is: $Param2"
  Write-Host "Param3 is optional. Current value is: $Param3"
}
 
Function Test2 {
  Write-Host "Total Arguments: $($Args.Length)"
  ForEach ($Arg in $Args) {
    $i++; Write-Host "$i : $Arg"
  }
}
 
# Command to call the functions above
 
# Named Paramaters
Test1 -Param1 "Bob"
Test1 -Param1 "Bob" -Param2 "Tim"
Test1 -Param2 "Alan"
Test1 -Param2 "Beth" -Param3 "Rachel"
 
# Positional Parameters
Test2 "Bob" "Tim"
Test2 "Bob"

Open in new window

0
 
SiHodgy007Author Commented:
Again Chris I don't understand, I have to pass two parameters from the command line into the script.  for zipping

ie test.ps1 detination sourcefile


$7Zip = 'C:\"Program Files"\7-Zip\7z.exe '
$Archive = "param1"
$Path = "param2"
 
Get-ChildItem $Path | ?{ $_.Name -like "*.txt" } | %{
  Invoke-Expression "$7Zip a -mx=9 $Archive $($_.FullName) "
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.