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: 7
      Replies: 8
      Points: 139
      Rank: Participant

      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

      [email protected](‘_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: 13
      Replies: 1691
      Points: 2,882
      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: 31
      Replies: 837
      Points: 2,629
      Helping Hand
      Rank: Community Hero

      Wouldn’t it be fullname not name?

    • #198269
      Participant
      Topics: 5
      Replies: 325
      Points: 499
      Helping Hand
      Rank: Contributor
      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
  • The topic ‘exclude directories in more professional way.’ is closed to new replies.