2

I'm writing a simple powershell script, and I don't understand its behavior. Here is the code.

Function print($first,$second){
Write-Host "$first"
}

$one="Dog"
$two="Cat"

print($one,$two)

And here is the output.

Dog Cat

I don't know why its printing both parameters instead of just the one that I asked for. I found a similar question posted that says the answer to the solution is to write

print $one $two

But I don't know why. The other question is at How do I pass multiple string parameters to a PowerShell script?

Can anyone illuminate on this subject?

1 Answer 1

6

Like you mentioned, you have to call it as:

print $one $two

In Powershell, arguments to functions are delimited with space and not comma and are not enclosed in parentheses ( method arguments are, though)

The way you were calling, print($one,$two), it is like calling print with one argument which is an array - ($one,$two). So when you write-host $first, you are echoing the array and hence you see them both.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.