Recently I had to write some scripts in PowerShell. Because I try to follow good software development practices I decided to split script logic into functions. First I defined a few functions that taken or returned single value:
Of course they worked brilliantly:
But then I needed to declare function that takes three arguments, I started with code:
Unfortunatelly it didn’t work as expected:
After a bit of research it turned out that PowerShell uses space as argument separator. So to pass three arguments we need to write:
This also works with single argument functions:
Before we end let’s find out what exactly is
(1, 2, 3)
and why calling
It turns out that
(a, b, ..., z) expression represents array of objects in PowerShell:
(x) expression represents just
x value :
and thats explain outputs of
manyArgs(1, 2, 3) command.
This behaviour of PowerShell may be surprising for C# programmers, I hope this post will help you remember about this problem.