Passing a variable to a powershell script via command line
Asked Answered
O

5

59

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.

Othelia answered 7/5, 2013 at 19:6 Comment(1)
Possible duplicate of How to handle command-line arguments in PowerShellMango
U
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.

Upturned answered 7/5, 2013 at 19:13 Comment(4)
@MichaelHedgpeth: It looks like it was a temporary problem; it's back up now. I don't know of a more permanent link for the article.Upturned
Personally the $args parameter is easier. :)Pal
The link is broken again :-(Fleabite
@MichaëlPolla: I wish they would stop moving the article! I fixed the link again.Upturned
U
80

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)

Unsocial answered 7/5, 2013 at 19:12 Comment(3)
Id rather call it with ./Test.ps1 -a="Here is your text" but its printing $a like this: -a=Here is your stringLettielettish
@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.Concelebrate
Writing it in first line is important as it gives error if param is not in first line.Will
U
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.

Upturned answered 7/5, 2013 at 19:13 Comment(4)
@MichaelHedgpeth: It looks like it was a temporary problem; it's back up now. I don't know of a more permanent link for the article.Upturned
Personally the $args parameter is easier. :)Pal
The link is broken again :-(Fleabite
@MichaëlPolla: I wish they would stop moving the article! I fixed the link again.Upturned
O
14

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'"
Octavla answered 7/4, 2017 at 12:57 Comment(0)
B
7

Passed parameter like below,

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

.\script_name.ps1 -Name name -Key key

Battleplane answered 26/11, 2018 at 5:51 Comment(2)
How does the help message come in?Brooklynese
@Brooklynese Actually help text will be useful if user doesn't know how to execute. For getting that help text you have to type !? without passing any input.Battleplane
K
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"
Knobloch answered 27/7, 2018 at 21:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.