1

Im trying to pass some arguments from the windows run while calling a powershell script. Looks something like this: myscript "Some parameters", "some other" Is this even possible? If so, how can I take the arguments from it to the powershell script and use them? So far I got how you can ask for a user input parameter via cmd using the "ValueFromPipelineByPropertyName" option of the Parameter but it's not what I want. Thanks in advance.

3 Answers 3

2

PowerShell essentially provides two ways for handling script arguments:

  • The automatic variable $args holds a list of all arguments, which can then be accessed by index:

    Script:

    "1st argument: " + $args[0]
    "2nd argument: " + $args[1]
    "3rd argument: " + $args[2]
    

    Invocation:

    powershell.exe -File .\script.ps1 "foo" "bar"
    

    Output:

    1st argument: foo
    2nd argument: bar
    3rd argument: 
    
  • A Param() section at the beginning of the script gets the parameter values assigned to individual variables:

    Script:

    Param(
      [Parameter()]$p1 = '',
      [Parameter()]$p2 = '',
      [Parameter()]$p3 = ''
    )
    
    "1st argument: " + $p1
    "2nd argument: " + $p2
    "3rd argument: " + $p3
    

    Invocation:

    powershell.exe -File .\script.ps1 "foo" "bar"
    

    Output:

    1st argument: foo
    2nd argument: bar
    3rd argument: 
    

If you want to be able to invoke a PowerShell script without explicitly running the powershell.exe command you need to change the default action for the Microsoft.PowerShellScript.1 type in the registry, though. You probably also need to adjust the execution policy on your system(s) (Set-ExecutionPolicy RemoteSigned -Force).

Typically you'd use $args only for very simple scenarios (a handful arguments in well-defined order). A full parameter definition gives you far better control about the parameter handling (you can make parameters optional or mandatory, define parameter types, define default values, do validation, etc.).

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I looked for. Thanks :)
0

I barely understand your question. Here's an attempt to get closer to what you want..

You attempt to call a powershell script through the Windows CMD, like so:

powershell.exe myscript.ps1 parameter1 parameter2 anotherparameter

The above is how you can take unnamed parameters. You could also look into named parameters, like so:

Powershell.exe myscript.ps1 -param1 "Test" -param2 "Test2" -anotherparameter "Test3"

You can in the CMD Accept input from a user using "Set", like so:

set /p id="Enter ID: "

In powershell you would use Read-host, like so:

$answer = Read-Host "Please input the answer to this question"

1 Comment

Further reading on named parameters: powershell-guru.com/…
0

On the top of your script you declare the parameters you want to pass, here is an example from my build.ps1

param (
    [string] $searchTerm,
    [ValidateSet('Remote', 'Local')][string] $sourceType = 'local',
    [switch] $force
)

Write-Host $searchTerm

You can then pass your parameters either by the order:

build.ps1 '1234' local -force

or with named paramters

build.ps1 -searchTerm '1234' -sourceType local -force

1 Comment

I tried doing that but it didn't work. Maybe I missed something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.