0

This is how my Input looks like:

$AllDrives = '[
    {
        "DriveLetter":  "F",
        "FileSystemLabel":  "Drive_F",
        "AllocationUnitSize":  4096
    },
    {
        "DriveLetter":  "E",
        "FileSystemLabel":  "Drive_E",
        "AllocationUnitSize":  4096
    },
    {
        "DriveLetter":  "H",
        "FileSystemLabel":  "",
        "AllocationUnitSize":  65536
    },
    {
        "DriveLetter":  "I",
        "FileSystemLabel":  "",
        "AllocationUnitSize":  65536
    },
    {
        "DriveLetter":  "G",
        "FileSystemLabel":  "",
        "AllocationUnitSize":  65536
    }
]' | ConvertFrom-Json

I want to compare another array with $AllDrives and return those elements from another array where DriveLetter is not matching with them.

$StandardDrives = @("E","F","G","H","I","J","K")

How do I accomplish this in (PowerShell way) without running two loops ?

Expected output is @("J","K")

0

3 Answers 3

3

Use Where-Object and take advantage of property enumeration:

$StandardDrives |Where-Object {$_ -notin $AllDrives.DriveLetter}

When PowerShell sees that the $AllDrives array object does not have a DriveLetter property, it'll automatically enumerate the values of DriveLetter on the individual array items instead, and the expression $AllDrives.DriveLetter thus expands to an array equivalent to @('F','E','H','I','G')

Sign up to request clarification or add additional context in comments.

5 Comments

Nicely done. Re terminology: Regrettably, the docs do not give the feature an official name. While the term property enumeration is reasonable, the more general term is member enumeration, given that the feature also works with methods. Member enumeration is indeed the term the person who actually implemented the feature coined in the blog post announcing it.
@mklement0 The term "property enumeration" pre-dates "member enumeration" by 1 month then - it's the term used in the "New Features in Windows PowerShell 3.0" - part of the on-box documentation released with Windows Server 2012, RC build of which dropped in mid-May 2012 ¯\_(ツ)_/¯
Good to know. But this is isn't a who-came-first issue. If member doesn't make sense to you as the proper abstraction encompassing both properties and methods, then ¯_(ツ)_/
The larger point is: The feature doesn't have an official name in the docs (blog posts don't count), but it should. I've tried to make the docs team introduce one, to no avail. In other cases, they did - whether I liked the name ultimately chosen or not ("intrinsic members"). In the absence of an official name, it would make sense for high-profile answerers here to agree on a name, hence my comment. Testy responses such as yours (a) suggest a lack of awareness that an agreed-upon term is important and (b) prevent reaching an agreement.
@mklement0 Fair point. FWIW I agree "member enumeration" is indeed the appropriate way to describe the abstract operation, but I think in this specific case, "property enumeration" is a better label because I'm specifically showcasing implicit enumeration of properties (no second-level methods were invoked during the course of this answer ^_^). I really do appreciate you trying to establish some common ground in describing this core aspect of PowerShell's behavior, but I think the appropriate route is through the docs :)
2

here is yet another way to get the drive letters that are not in use. [grin]

what it does ...

  • defines the list of possible drive letters
  • uses the .Where() collection method to filter things
  • checks to see if the possible letter is in the in-use letter collection
  • outputs that to the success stream
    that can be captured to a $Var by assigning it to one.

the code ...

@('e', 'f', 'g', 'h', 'i', 'j', 'k').Where({
    $_ -notin $AllDrives.DriveLetter
    })

output ...

j
k

Comments

1

Using Compare-Object:

(Compare-Object -ReferenceObject $AllDrives.DriveLetter -DifferenceObject $StandardDrives).InputObject

will return:

J
K

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.