exclude directories in more professional way.

Welcome Forums General PowerShell Q&A exclude directories in more professional way.

Viewing 3 reply threads
  • Author
    Posts
    • #198056
      Participant
      Topics: 3
      Replies: 4
      Points: 66
      Rank: Member

      I am trying to exclude some directories when using get-childitem

      I know this way will

      Get-ChildItem c:\users |where {$_.name -NotLike "*SUP" -and $_.name -notlike "Cit*"}

      Get-ChildItem c:\users |where {$_.name -Notmatch "SUP|Cit|pcg"}

      I am trying to make it looks more nicer and easy to change in future

      $e=@('_sup','sa','pcg','srv,'citrix','administrator')

      Get-ChildItem c:\users |where {$_.name -NotLike $e }

      OR

      $e="_sup|sa|citrix"

      Get-childitem c:\users \where {$_.name -notmatch $e

      It doesn't exclude any directory. can someone help?

    • #198062
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero

      The following work for me:

      $exclusion = 'Sales|Corp'
      
      Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch $exclusion}
      

      or

      $exclusion = 'Sales','Corp'
      
      Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch ($exclusion -join '|')}
      

      The directories are:

      PS C:\WINDOWS\system32> Get-ChildItem C:\Scripts -Directory
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Corp                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Legal                                                                                                                                                                                                           
      d-----        12/6/2019   9:04 AM                Sales 
      

      Both methods filter with the result:

      PS C:\WINDOWS\system32> 
      $exclusion = 'Sales','Corp'
      
      Get-ChildItem C:\Scripts -Directory | where {$_.name -Notmatch ($exclusion -join '|')}
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Legal
      
    • #198065
      js
      Participant
      Topics: 27
      Replies: 739
      Points: 2,015
      Helping Hand
      Rank: Community Hero

      Wouldn't it be fullname not name?

    • #198269
      Participant
      Topics: 5
      Replies: 285
      Points: 213
      Helping Hand
      Rank: Participant
      Get-ChildItem \\path\test -Exclude FolderT,FileW
      
      # Exclude folder and file in array
      $list = @('FolderT','FileW')
      Get-ChildItem \\path\test -Exclude $list
      
Viewing 3 reply threads
  • You must be logged in to reply to this topic.