1

I have a PowerShell query.

I have the below 2 string arrays. What I want to be able to do is to return only those entries from $RecycleBinPaths that do not begin with all the entries from $ExcludeRecycleBinsPathWildCards (i.e. Wildcard pattern match)

$RecycleBinPaths = @('C:\$Recycle.Bin','D:\$RECYCLE.BIN','E:\$RECYCLE.BIN','F:\$RECYCLE.BIN','L:\$RECYCLE.BIN','M:\$RECYCLE.BIN','S:\$RECYCLE.BIN','T:\$RECYCLE.BIN')

$ExcludeRecycleBinsPathWildCards = @('C:\','F:\')

In the above example I want the output to be:

D:\$RECYCLE.BIN
E:\$RECYCLE.BIN
L:\$RECYCLE.BIN
M:\$RECYCLE.BIN
S:\$RECYCLE.BIN
T:\$RECYCLE.BIN

In fact $ExcludeRecycleBinsPathWildCards array above can contain any number of entries, not necessarily just 2 as shown above.

Are you able to help? Thanks.

2 Answers 2

2

Use nested .Where() methods:

$RecycleBinPaths.Where({
  # Save path to separate variable
  $path = $_
  # Now test if _any_ string in the list of wildcards match, and then apply -not - making it resolve to true only if _none_ of the wildcards match the path
  -not $ExcludeRecycleBinsPathWildCards.Where({ $path -like "$_*" }, 'First')
})
Sign up to request clarification or add additional context in comments.

5 Comments

Wow. You just blew my mind though I still haven't understood what your code is doing, Thanks for your help. have a nice day.
@Steve You're welcome! I've added some inline comments to help explain better. If this answer solves your problem, please consider marking it accepted by clicking the checkmark on the left :)
Does this .Where method exist on PowerShell 3 for arrays/collections? If not, is there a workaround for PS3? We have a mixture of PS version in the estate including 3
@Steve I think it was introduced in 4.0, if memory serves me right, but I can't remember clearly - for earlier versions you can achieve the same with the Where-Object cmdlet
Thank you. You have taught me something new today. I never knew Where-Object could do more than just simple comparison :-) $RecycleBinPaths | Where-Object {$path = $_; -not ($ExcludeRecycleBinsPathWildCards | Where-Object {$path -like "$_*" })}
0

Another way of looping through the items and adding to another array.

$RecycleBinPaths = @('C:\$Recycle.Bin','D:\$RECYCLE.BIN','E:\$RECYCLE.BIN','F:\$RECYCLE.BIN','L:\$RECYCLE.BIN','M:\$RECYCLE.BIN','S:\$RECYCLE.BIN','T:\$RECYCLE.BIN')

$ExcludeRecycleBinsPathWildCards = @('C:\','F:\')

$SelectedRecycleBinPaths = @()

foreach($rbp in $RecycleBinPaths)
{

    if(-Not ($ExcludeRecycleBinsPathWildCards -contains $rbp.ToString().Substring(0,3)))
    {
        $SelectedRecycleBinPaths += $rbp
    }

} 

$SelectedRecycleBinPaths
D:\$RECYCLE.BIN
E:\$RECYCLE.BIN
L:\$RECYCLE.BIN
M:\$RECYCLE.BIN
S:\$RECYCLE.BIN
T:\$RECYCLE.BIN

1 Comment

Thanks but your solution above assumes each of the $ExcludeRecycleBinsPathWildCards entries is exactly 3 characters long? What if it was arbitrary length?

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.