Passing a variable to a powershell script via command line

PowershellPowershell Ise

Powershell Problem Overview


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.

Powershell Solutions


Solution 1 - Powershell

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)

Solution 2 - Powershell

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.

Solution 3 - Powershell

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'"

Solution 4 - Powershell

Passed parameter like below,

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

> .\script_name.ps1 -Name name -Key key

Solution 5 - Powershell

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncquadriniView Question on Stackoverflow
Solution 1 - PowershellSolaflexView Answer on Stackoverflow
Solution 2 - PowershellBrian StephensView Answer on Stackoverflow
Solution 3 - PowershellShilpa11View Answer on Stackoverflow
Solution 4 - PowershellkalaivaniView Answer on Stackoverflow
Solution 5 - PowershellmxmossView Answer on Stackoverflow