summaryrefslogtreecommitdiffhomepage
path: root/powershell.html.markdown
diff options
context:
space:
mode:
authorMichaƫl Peeters <xeyownt@users.noreply.github.com>2024-02-25 23:36:47 +0100
committerGitHub <noreply@github.com>2024-02-25 23:36:47 +0100
commitd123cd4a5c6599401e20c5d4190173d75cddd520 (patch)
tree6816e5a3195e985770a8705f8da3982b775b58b4 /powershell.html.markdown
parent51efc479330cb85c919222af054e82fb9f7e47d5 (diff)
[powershell/en] Fix and clarify -eq operator on (some) objects (#4333)
Diffstat (limited to 'powershell.html.markdown')
-rw-r--r--powershell.html.markdown16
1 files changed, 14 insertions, 2 deletions
diff --git a/powershell.html.markdown b/powershell.html.markdown
index 0383035b..2e7539a5 100644
--- a/powershell.html.markdown
+++ b/powershell.html.markdown
@@ -118,14 +118,15 @@ $False - 5 # => -5
2 -lt 3 -and 3 -lt 2 # => False
# (-is vs. -eq) -is checks if two objects are the same type.
-# -eq checks if the objects have the same values.
+# -eq checks if the objects have the same values, but sometimes doesn't work
+# as expected.
# Note: we called '[Math]' from .NET previously without the preceeding
# namespaces. We can do the same with [Collections.ArrayList] if preferred.
[System.Collections.ArrayList]$a = @() # Point a at a new list
$a = (1,2,3,4)
$b = $a # => Point b at what a is pointing to
$b -is $a.GetType() # => True, a and b equal same type
-$b -eq $a # => True, a and b values are equal
+$b -eq $a # => None! See below
[System.Collections.Hashtable]$b = @{} # => Point a at a new hash table
$b = @{'one' = 1
'two' = 2}
@@ -154,6 +155,13 @@ $age = 22
"$name's name is $($name.Length) characters long."
# => "Steve's name is 5 characters long."
+# Strings can be compared with -eq, but are case insensitive. We can
+# force with -ceq or -ieq.
+"ab" -eq "ab" # => True
+"ab" -eq "AB" # => True!
+"ab" -ceq "AB" # => False
+"ab" -ieq "AB" # => True
+
# Escape Characters in Powershell
# Many languages use the '\', but Windows uses this character for
# file paths. Powershell thus uses '`' to escape characters
@@ -274,6 +282,10 @@ $array.AddRange($otherArray) # Now $array is [1, 2, 3, 4, 5, 6]
# Examine length with "Count" (Note: "Length" on arrayList = each items length)
$array.Count # => 6
+# -eq doesn't compare array but extract the matching elements
+$array = 1,2,3,1,1
+$array -eq 1 # => 1,1,1
+($array -eq 1).Count # => 3
# Tuples are like arrays but are immutable.
# To use Tuples in powershell, you must use the .NET tuple class.