2

I tried using grave accent and curly brackets with ampersand but still can't split the function call line into several (or at least two) lines (which is useful when the call spans beyond the screen margin).

Let's take this function definition for example:

function Test-FunctionCall
{
  param
  (
    [Parameter(Mandatory=$true)]
    [string]$nickname,
    [Parameter(Mandatory=$true)]
    [string]$firstName,
    [Parameter(Mandatory=$true)]
    [string]$lastName
  )
  $nickname
  $firstName
  $lastName
}

How do you split the following line for it to still work as a function call?

Test-FunctionCall -nickname "Average" -firstName "Joe" -lastName "Smith"

Thanks

EDIT: duplicate of How to split long commands over multiple lines in PowerShell

1 Answers1

2

I'd recommend using splatting which takes a hashtable (@{ }) and applies it to the parameters of a command (in this case; it can also be used against .exe calls to apply an array (@( )) of arguments):

$testFunctionCallArgs = @{
    nickname  = 'Average'
    firstName = 'Joe'
    lastName  = 'Smith'
}
Test-FunctionCall @testFunctionCallArgs

For completeness sake about my .exe comment:

$pingArgs = @(
    '-t', '-a', '-4', '-r', '5', '127.0.0.1'
)
& PING.exe @pingArgs
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63