Question : 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

Answer : How do I pass 2 parameters into a powershell script?


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
1:
2:
3:
4:
5:
6:
7:
8:
# 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 Select All
Random Solutions  
 
programming4us programming4us