0

In Powershell, I need to create a set of unique strings. Powershell's standard array collection allows for duplicate strings. Powershell's standard hashtable collection allows for unique keys but requires values. How do I create a set in Powershell, a collection that has no duplicate values?

The script I'm running queries SCCM Device Collections. I need a list of all the hosts in a select group of Device Collections. Because a host can be a member of more than one collection, loading the values in an array creates many duplicates. Here's some sample data:

Device Collection 1.
    Server1
    Server2
    Server3
Device Collection 2.
    Server1
Device Collection 3.
    Server3
    Server4
    Server5

What I need:

$host_list = 'Server1','Server2','Server3','Server4','Server5'

What I get with a regular array:

$host_list = 'Server1','Server2','Server3','Server1','Server3','Server4','Server5'

I assume there's a "Set" collection type in .Net that I can use. Can you help me find the right one?

3
  • 3
    Have you check this Commented Feb 27, 2023 at 13:55
  • That's it! Sorry my searching skills weren't up to snuff today :) Commented Feb 27, 2023 at 14:15
  • 1
    Or use a string array and | Sort-Object -Unique Commented Feb 27, 2023 at 14:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.