Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line arguments to a PowerShell ps1 file

For years, I have used the cmd/DOS/Windows shell and passed command-line arguments to batch files. For example, I have a file, zuzu.bat and in it, I access %1, %2, etc. Now, I want to do the same when I call a PowerShell script when I am in a Cmd.exe shell. I have a script, xuxu.ps1 (and I've added PS1 to my PATHEXT variable and associated PS1 files with PowerShell). But no matter what I do, I seem unable to get anything from the $args variable. It always has length 0.

If I am in a PowerShell shell, instead of cmd.exe, it works (of course). But I'm not yet comfortable enough to live in the PowerShell environment full time. I don't want to type powershell.exe -command xuxu.ps1 p1 p2 p3 p4. I want to type xuxu p1 p2 p3 p4.

Is this possible, and if so, how?

The sample I cannot get to work is trivial, foo.ps1:

Write-Host "Num Args:" $args.Length; foreach ($arg in $args) {     Write-Host "Arg: $arg"; } 

The results are always like this:

C:\temp> foo Num Args: 0 C:\temp> foo a b c d Num Args: 0 c:\temp> 
like image 311
Daniel 'Dang' Griffith Avatar asked Aug 18 '09 13:08

Daniel 'Dang' Griffith


People also ask

How do you pass arguments on ps1?

Use param to Pass an Argument to a PowerShell Script We can define arguments using the param statement. It also allows us to use the default values. Here, the variable $address has a default value. And the default value will be used if the user does not provide any value.

How do I pass a command line argument in PowerShell?

Powershell will process and assign the arguments in the order they're given, unless overridden by using the proper parameter name, e.g., if your param block lists: $user $pass $server, and you execute yourscript. ps1 a b c, a will be set into $user, b into $pass and c into $server, UNLESS you specifically assign them!

How do I call a PowerShell script with parameters?

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.

How do I run a ps1 script from the PowerShell command line?

Running a script with PowerShell To open the PowerShell console, click on the Start button (or search button), type powershell, and click Run as Administrator. To run a script in the PowerShell console, you can either: Use the full path to script, like: C:\TEMP\MyNotepadScript. ps1.


1 Answers

This article helps. In particular, this section:

-File

Runs the specified script in the local scope ("dot-sourced"), so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters.

i.e.

powershell.exe -File "C:\myfile.ps1" arg1 arg2 arg3 

means run the file myfile.ps1 and arg1 arg2 & arg3 are the parameters for the PowerShell script.

like image 146
Arj Avatar answered Oct 03 '22 09:10

Arj