-Notlike not working for comparing to array - Powershell

ALVES Ricardo 106 Reputation points
2022-12-07T14:13:16.137+00:00

Hello Guys,

I did a script where I can list all items from a SharePoint site

I want to exclude some kind of files (ex: PNG, PDF, etc)

$SystemItem =@(".PDF", ".PNG")
$Item = $ListItems | Where {$_["FileLeafRef"] -notlike $SystemItem}

this command is not working, I keep receiving pdf's and png's files

If I change the variable $SystemItem to $SystemItem =@("*.pdf"), the script runs perfectly, any pdf's files show up
once I use the variable $SystemItem as an Array it doesn't work

Anyone knows How can I use "-notlike" with arrays?

Thank you so much
Cheers

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Hoekstra Jelle 501 Reputation points
    2022-12-07T15:12:23.94+00:00

    I think the M365 community could help you out best, Regardless, let me give it a go

    $SystemItems =@(".PDF", ".PNG")
    $notlikelist = foreach ($systemitem in $SystemItems){
    $Item = $ListItems | Where {$_["FileLeafRef"] -notlike $systemitem}
    $Item
    }
    $notlikelist

    For their community please check https://techcommunity.microsoft.com/t5/microsoft-365/ct-p/microsoft365

    Hope this helps!

    ----------

    If it solves your question, please accept the answer and upvote!

    0 comments No comments

  2. MotoX80 35,716 Reputation points
    2022-12-07T15:32:15.127+00:00

    Try this.

    $Item = $ListItems | Where {$_["FileLeafRef"] -notmatch ".pdf|.png"}  
    
    0 comments No comments

  3. Rich Matheisen 47,781 Reputation points
    2022-12-07T15:33:07.073+00:00

    If you compare a scalar to an array you aren't comparing the individual elements of the array -- you're comparing the object that contains the array elements.

    Try it this way:

    $Item = $ListItems | Where-Object {$SystemItems -notcontains $_["FileLeafRef"]}  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.