PowerShell: Default Value of [Int]$a is 0

I didn’t realise until yesterday that the default value of a variable using the [int] type had a default value of 0, e.g


\[Int\]$a

So what’s the big deal? Say you have a function as follows:


function Test-DefaultInt {

Param (

\[Int\]$a,

\[Int\]$b,

\[Int\]$c )

"a is $a" "b is $b" "c is $c" }

and run it like so


Test-DefaultInt -a 5 -b 6 -c 7

Test-DefaultInt -a 5 -b 6

You get the following results:

If you change the function and take away the [int] from $c:


function Test-DefaultInt {

Param (

\[Int\]$a,

\[Int\]$b,

$c )

"a is $a" "b is $b" "c is $c" }

and run the same tests you get the following:

with no value for c. OK, its a contrived example, but I had a reasonably complex situation to deal with. The difference between a variable being the value 0 or not present, inside the workings of the function, made a big difference to the output. Having wasted about an hour last night on it, I hope this saves someone else a headache :-)