58

I am new to powershell, and trying to teach myself the basics. I need to write a ps script to parse a file, which has not been too difficult.

Now I want to change it to pass a variable to the script. that variable will be the parsing string. Now, the variable will always be 1 word, and not a set of words or multiple words.

This seems uber simple yet is posing a problem for me. Here is my simple code:

$a = Read-Host
Write-Host $a

When I run the script from my command line the variable passing doesn't work:

.\test.ps1 hello
.\test.ps1 "hello"
.\test.ps1 -a "hello"
.\test.ps1 -a hello
.\test.ps1 -File "hello"

As you can see, I have tried many methos with no success, of the script taking the value an outputting it.

The script does run, and waits for me to type a value, and when I do, it echos that value.

I just want it to output my passed in value, what minuscule thing am I missing?

Thank you.

Solaflex
  • 1,382
  • 1
  • 13
  • 24
cquadrini
  • 769
  • 2
  • 12
  • 25
  • 2
    Possible duplicate of [How to handle command-line arguments in PowerShell](http://stackoverflow.com/questions/2157554/how-to-handle-command-line-arguments-in-powershell) – Michael Freidgeim Feb 03 '16 at 11:55

5 Answers5

76

Make this in your test.ps1, at the first line

param(
[string]$a
)

Write-Host $a

Then you can call it with

./Test.ps1 "Here is your text"

Found here (English)

Greg
  • 404
  • 4
  • 15
Solaflex
  • 1,382
  • 1
  • 13
  • 24
  • Id rather call it with ./Test.ps1 -a="Here is your text" but its printing $a like this: -a=Here is your string – Andy Feb 21 '17 at 23:06
  • @ozzy432836 I too prefer that syntax, but it is not built into powershell. Without a space PS sees it as a single unnamed argument. You can certainly implement your own argument parsing, but you might consider that it will not be what anyone else expects. Powershell's built-in functionality allows for named and unnamed (aka positional) arguments, mandatory and optional arguments with default values and automatically generates help. That is a lot to throw away because you (& I) prefer an '=' over a space. I wasted my time re-inventing this wheel in C# Few of my users could care. – Andrew Dennison Feb 27 '18 at 03:23
  • Writing it in first line is important as it gives error if param is not in first line. – Deepak Yadav Dec 27 '19 at 05:38
56

Here's a good tutorial on Powershell params:

PowerShell ABC's - P is for Parameters

Basically, you should use a param statement on the first line of the script

param([type]$p1 = , [type]$p2 = , ...)

or use the $args built-in variable, which is auto-populated with all of the args.

Brian Stephens
  • 5,161
  • 19
  • 25
13

Declare the parameter in test.ps1:

 Param(
                [Parameter(Mandatory=$True,Position=1)]
                [string]$input_dir,
                [Parameter(Mandatory=$True)]
                [string]$output_dir,
                [switch]$force = $false
                )

Run the script from Run OR Windows Task Scheduler:

powershell.exe -command "& C:\FTP_DATA\test.ps1 -input_dir C:\FTP_DATA\IN -output_dir C:\FTP_DATA\OUT"

or,

 powershell.exe -command "& 'C:\FTP DATA\test.ps1' -input_dir 'C:\FTP DATA\IN' -output_dir 'C:\FTP DATA\OUT'"
Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
Shilpa11
  • 131
  • 1
  • 4
7

Passed parameter like below,

Param([parameter(Mandatory=$true,
   HelpMessage="Enter name and key values")]
   $Name,
   $Key)

.\script_name.ps1 -Name name -Key key

Macke
  • 24,812
  • 7
  • 82
  • 118
kalaivani
  • 484
  • 1
  • 5
  • 8
3

Using param to name the parameters allows you to ignore the order of the parameters:

ParamEx.ps1

# Show how to handle command line parameters in Windows PowerShell
param(
  [string]$FileName,
  [string]$Bogus
)
write-output 'This is param FileName:'+$FileName
write-output 'This is param Bogus:'+$Bogus

ParaEx.bat

rem Notice that named params mean the order of params can be ignored
powershell -File .\ParamEx.ps1 -Bogus FooBar -FileName "c:\windows\notepad.exe"
mxmoss
  • 449
  • 4
  • 8