Update: The problem turned out to be the lack of initialization of $fullnames as an array, with $fullnames = @() (in the absence of this initialization, += perform simple string concatenation instead of appending an element to an array, conceptually speaking).
The answer below may still be of interest for diagnosing how identical-looking strings may differ.
Since your Sort-Object inputs are strings (and the name-per-line output implies that $fullnames is indeed an array, even though you don't show its initialization as such), the implication is that the ostensible duplicates must differ by whitespace (type) or invisible control characters.
You can try the following technique to discover such invisible discrepancies:
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
ForEach-Object { $_ + "`t" + [int[]] [char[]] $_ } |
Sort-Object -Unique
Output:
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107 32 # Note the extra 32
Frank Coutel 70 114 97 110 107 160 67 111 117 116 101 108 # Note the 160
Frank Coutel 70 114 97 110 107 32 67 111 117 116 101 108
The numbers are the Unicode code points of the characters that make up each string, in decimal format.
Alternatively, for a better visualization of the differences, use helper function Debug-String from this Gist:
- Note: The following snippet downloads and defines the function automatically. I can personally assure you that doing so is safe, but you you should always check the source code yourself first.
# Download and define helper function Debug-String
# * See comments above.
# * To see instructions on how to make the function available in
# *future* sessions, remove >3 $null and re-run this command directly
# from the command line.
irm https://gist.github.com/mklement0/7f2f1e13ac9c2afaf0a0906d08b392d1/raw/Debug-String.ps1 | iex 3>$null
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
Debug-String |
Sort-Object -Unique
Output:
Adam·Warlock
Adam·Warlock· # Note the trailing ·
Frank·Coutel
Frank`u{a0}Coutel # Note the `u{a0} escape sequence
Debug-String visualizes regular spaces as ·, and formatting/control characters outside the ASCII range as Unicode escape sequences, e.g. `u{a0}, where a0 is the hex form of the character's Unicode code point, namely of the no-break space ( U+00A0) character, in this case.
get-loggedinuser?