Powershell Exe Arguments

Active3 months ago

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.

Aug 14, 2018  PowerShell command line options.; 2 minutes to read; In this article. You can use command line options to run PowerShell from the command line of another tool, such as Cmd.exe or bash, or use it at the PowerShell command line to start a new session. The command line options allow you to customize the configuration of the PowerShell session and control the input. 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).

Is this possible, and if so, how?

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

The results are always like this:

YoungHobbit
11.1k8 gold badges38 silver badges64 bronze badges
Daniel 'Dang' GriffithDaniel 'Dang' Griffith
9331 gold badge9 silver badges13 bronze badges

7 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.

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

ArjArj
1,1353 gold badges16 silver badges40 bronze badges

OK, so first this is breaking a basic security feature in PowerShell. With that understanding, here is how you can do it:

  1. Open an Windows Explorer window
  2. Menu Tools ->Folder Options -> tab File Types
  3. Find the PS1 file type and click the advanced button
  4. Click the New button
  5. For Action put: Open
  6. For the Application put: 'C:WINNTsystem32WindowsPowerShellv1.0powershell.exe' '-file' '%1' %*

You may want to put a -NoProfile argument in there too depending on what your profile does.

Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges
EBGreenPowershell.exe pass arguments to scriptPowershellEBGreen
27.6k11 gold badges54 silver badges78 bronze badges
Parameters

After digging through the PowerShell documentation, I discovered some useful information about this issue. You can't use the $args if you used the param(...) at the beginning of your file; instead you will need to use $PSBoundParameters. I copy/pasted your code into a PowerShell script, and it worked as you'd expect in PowerShell version 2 (I am not sure what version you were on when you ran into this issue).

If you are using $PSBoundParameters (and this ONLY works if you are using param(...) at the beginning of your script), then it is not an array, it is a hash table, so you will need to reference it using the key / value pair.

Powershell Call Exe Arguments

And when called with...

The result is...

Powershell Run Command With Arguments

Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges
Randall BorckRandall Borck

You could declare your parameters in the file, like param:

And then call the PowerShell file like so .temp.ps1 para1 para2....para10, etc.

V.BV.B

Powershell Start Exe Arguments

6116 gold badges23 silver badges43 bronze badges

Maybe you can wrap the PowerShell invocation in a .bat file like so:

If you then placed this file under a folder in your PATH, you could call PowerShell scripts like this:

Quoting can get a little messy, though:

Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges

Powershell Command Line Options

guillermoooguillermooo
4,42213 gold badges48 silver badges56 bronze badges

You may not get 'xuxu p1 p2 p3 p4' as it seems. But when you are in PowerShell and you set

You can run those scripts like this:

or

or

I hope that makes you a bit more comfortable with PowerShell.

Austin T French

Call Powershell Script From Command Line

3,0351 gold badge15 silver badges35 bronze badges
Atiq RahmanAtiq Rahman

if you want to invoke ps1 scripts from cmd and pass arguments without invoking the script like

you can do the following

This is assuming powershell.exe is in your path

ta32ta32

Not the answer you're looking for? Browse other questions tagged command-linepowershellarguments or ask your own question.