See this SO thread
PowerShell and the -contains operator
Given:
function Add-Numbers([int]$a, [int]$b) {
return $a + $b
}The code:
echo Add-Numbers(3, 2)Prints:
Add-Numbers
3
2
But
echo (Add-Numbers 3 2) # => 5$a = 4
echo "$a" # => 4
echo "$($a)" # => 4
echo "$($($a))" # => 4function Maskify([string]$cc) {
$res = ""
$cc = $cc.ToCharArray()
foreach ($c in $cc) {
$res += "#"
}
$res
}
echo (Maskify "1234") # => #This seems like it should loop 4 times and return "####" but only loops once. Possible fixes include removing the [string] type on the parameter or using $ccc = rather than $cc = and using $ccc in the foreach.
echo ([int]1.5) # => 2, not 1$hash = @{A = 42; B = 1}
$s = "AB"
echo $s[0] # => A
echo $hash[$s[0]] # => null
echo $hash[[string]$s[0]] # => 42$arr = @(@(1, 2), @(3, 4))
echo $arr[0][0] # => 1
echo $arr[0][1] # => 2
echo $arr[1][0] # => 3
echo $arr[1][1] # => 4
echo $arr.length # => 2
$arr = @(@(1, 2))
echo $arr[0][0] # => 1
echo $arr[0][1] # => missing!!?
echo $arr.length # => 2 !!?
# correct:
$arr = @(,@(1, 2))
echo $arr[0][0] # => 1
echo $arr[0][1] # => 2
echo $arr.length # => 1It turns out the @ symbols and parens are sort of optional. The best generalization seems to be:
$arr = ,(1, 2), (3, 4)
# or
$arr = ,(,1, 2), (,3, 4)
$arr = ,(,(,1, 2)), (,(,3, 4))
echo $arr[0][0][0] # => 1
echo $arr[0][0][1] # => 2
echo $arr[1][0][0] # => 3
echo $arr[1][0][1] # => 4
echo $arr.length # => 2$arr = 1, 2
echo $arr[0] # => 1
echo $arr[0][0] # => 1
echo $arr[0][0][0] # => 1
echo $arr[0][0][0][0][0] # => 1
echo $arr[0][0][0][0][1] # => missing (`Unable to index into an object of type System.Int32` in strict mode)
echo $arr[0][-1][-1][-1][-1] # => 1function a() {
echo 41
42
return 43
}
echo a # => a (hah! just testing)
echo (a) # => 41 42 43Naturally this isn't the case with class methods:
class Test {
[int] a() {
echo 41
42
return 43
}
}
$x = [Test]::new()
echo ($x.a) # => OverloadDefinitions ... what?
echo $x.a() # => 43