0

The directory structure is as follows:

    Directory: \\L04274\C$\Temp


Mode                LastWriteTime         Length Name                                            
----                -------------         ------ ----                                            
d-----        3/12/2018   2:04 PM                AutomatedImportProcess                          
d-----        3/13/2018  11:34 AM                f1                                              
d-----        3/13/2018  11:33 AM                f2       

the exclusion CSV file contains one column (later might be extended to two columns which includes file types as well).

ExcludeDirectory
\\L04274\C$\Temp\f2\
\\L04274\C$\Temp\f1\ 

The PowerShell command looks like this but it does not seem to be doing what is expected:

$path = '\\L04274\C$\Temp\f1' 
$exclusion = @(Get-Content -LiteralPath '\\L04274\C$\Temp\Exclusions.csv')

Get-ChildItem -Path $path -Force -Exclude $exclusion

What I expect to see is only the following folder:

\\L04274\C$\Temp\AutomatedImportProcess\
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Fuzzy
  • 3,810
  • 2
  • 15
  • 33
  • Take a look at this question: https://stackoverflow.com/questions/15294836/how-can-i-exclude-multiple-folders-using-get-childitem-exclude – henrycarteruk Mar 13 '18 at 17:15
  • @JamesC. this is not quite what I want to do. in the question you mentioned they are using the following to exclude: [$archive = *archive*,*Archive*,*ARCHIVE*] where I want to read from a csv into an array or some list and use that list to exclude folders/files. So if I want to exclude the f1 folder then the entry "\\L04274\C$\Temp\f1\" should be used to exclude. and if I want to only exclude the text files then "\\L04274KAMRANF\C$\Temp\f1\*.txt" should be used. – Fuzzy Mar 13 '18 at 17:29
  • The first thing that I notice is that you do your Get-ChildItem at '\\L04274\C$\Temp\f1' but your exclusions have C:\Temp as their root. – EBGreen Mar 13 '18 at 17:47

2 Answers2

2

Exclude only works against file names.

You'll have to use a nested Where-Object statement after the fact:

$path = '\\L04274\C$\Temp\f1' 
$exclusions = @(Import-Csv -LiteralPath '\\L04274\C$\Temp\Exclusions.csv') |Select-Object -Expand ExcludeDirectory

Get-ChildItem -Path $path -Force |Where-Object {
    $FullName = $_.FullName
    -not($exclusions|Where-Object {
        $FullName -like "$_*"
    })
}

If the full path doesn't match any of the exclusions, the -not(...) statement will return $true, otherwise the file will be omitted

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks this threw an exception but it did give me an idea that led me to solve the issue: – Fuzzy Mar 13 '18 at 18:15
  • FYI exception was as follows: Select-Object : Property "ExcludeDirectory" cannot be found. At C:\Users\kamranf\Documents\PoweShellScripts\Untitled15.ps1:2 char:85 + ... RANF\C$\Temp\Exclusions.csv') |Select-Object -Expand ExcludeDirectory + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (ExcludeDirectory:PSObject) [Select -Object], PSArgumentException + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands .SelectObjectCommand – Fuzzy Mar 13 '18 at 18:15
  • @KamranFarzami My mistake, should have been `Import-Csv` rather than `Get-Content` – Mathias R. Jessen Mar 13 '18 at 18:19
0

This worked:

$path = '\\L04274\C$\Temp' 
$exclusion = @(Get-Content -LiteralPath '\\L04274\C$\Temp\Exclusions.csv')

 (Get-ChildItem -Path $path -Force -Exclude $exclusion | ForEach-Object -Process { 
  $allowed = $true 
  foreach ($exclude in $exclusion) { 
     if ( $_.FullName  -like $exclude) {  
       $allowed = $false
       break
    }

  }
  if ($allowed) {
    $_ 
  }
 }).FullName
Fuzzy
  • 3,810
  • 2
  • 15
  • 33
  • Note that `"\\L04274\C$\Temp\f2\file.txt" -like "\\L04274\C$\Temp\f2\"` is false. You'd need it to be `"\\L04274\C$\Temp\f2\file.txt" -like "\\L04274\C$\Temp\f2\*"` to be true. Not sure what you're after, however. – Bacon Bits Mar 13 '18 at 18:44