mark
mark

Reputation: 62876

What is the Powershell equivalent for bash $*?

In other words how can I get the command line of the script itself?

So, I know about $PSBoundParameters, but it is not the same. I just want to get the string containing the passed in parameters as is.

How do I do it?

Upvotes: 4

Views: 3882

Answers (3)

adamency
adamency

Reputation: 1538

just want to get the string containing the passed in parameters as is.

You're looking for a powershell equivalent of "$@", not $*.

And the other answers in the thread are not equivalent. The best quick way I have found for this is:

$args | join-string -sep ' '

which can in turn be used for any string array you may have on hand, not just the $args array.

Upvotes: 0

Kory Gill
Kory Gill

Reputation: 7163

See get-help about_Automatic_Variables.

$Args
   Contains an array of the undeclared parameters and/or parameter
   values that are passed to a function, script, or script block.
   When you create a function, you can declare the parameters by using the
   param keyword or by adding a comma-separated list of parameters in
   parentheses after the function name.

   In an event action, the $Args variable contains objects that represent
   the event arguments of the event that is being processed. This variable
   is populated only within the Action block of an event registration
   command.  The value of this variable can also be found in the SourceArgs
   property of the PSEventArgs object (System.Management.Automation.PSEventArgs)
   that Get-Event returns.

Example:

test.ps1

param (
)

Write-Output "Args:"
$args

Output:

PS L:\test> .\test.ps1 foo bar, 'this is extra'
Args:
foo
bar
this is extra

Upvotes: 4

JosefZ
JosefZ

Reputation: 30183

$MyInvocation.Line

Read about_Automatic_Variables:

$MyInvocation
   Contains an information about the current command, such as the name, 
   parameters, parameter values, and information about how the command was
   started, called, or "invoked," such as the name of the script that called
   the current command. 

   $MyInvocation is populated only for scripts, function, and script blocks.

Upvotes: 2

Related Questions