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\
3
  • Take a look at this question: stackoverflow.com/questions/15294836/… Commented Mar 13, 2018 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. Commented Mar 13, 2018 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. Commented Mar 13, 2018 at 17:47

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this threw an exception but it did give me an idea that led me to solve the issue:
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
@KamranFarzami My mistake, should have been Import-Csv rather than Get-Content
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

1 Comment

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.

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.