Help with excluding results from Get-Childitem RRS feed

  • Question

  • Hello,

    I am having an issue with a PowerShell script that I am working on. This script needs to gather the files paths to all files with a specific folder recursively, but I need to exclude all files found within 3 specific folders. It works great in getting me the files paths but I cannot seem to get the exclusion to work correctly. I would appreciate any assistance that could be provided. Below is the code

    Invoke-Command -Computername servername -ScriptBlock {Get-ChildItem -path "\\servername\driveletter\folder1\folder2" -recurse -file |
    Where-Object { $_.LastWriteTime -ge (Get-Date).addminutes(-30) -and $_.Extension -like ".pdf" -and ($_.FullName -notcontains "badfolder1") -and ($_.FullName -notcontains "badfolder2") -and ($_.FullName -notcontains "badfolder3")} |
    ForEach-Object { $_.FullName } | Out-File -FilePath D:\folder1\Script_results\Files.txt -encoding ASCII }

    Thank You

    • Edited by Cygnus21 Wednesday, October 10, 2018 5:31 PM
    Wednesday, October 10, 2018 3:29 PM

All replies

  • Use "-nomatch" rather than "-notcontains".  Contains is meant to compare a collection and the value would need to match exactly.  For example if you had a file called \\filleserver\share\badfolder\badfile.pdf, the way to exclude that with notcontains is the entire value of fullname.

    Also you should probably replace the FullName property with the Directory property.  You could have a file name containing the string "badfolder1" that is not nested under a directory with the string "badfolder1".  Based on your rules this file should be included, but matching based on FullName would exclude it. 

    Wednesday, October 10, 2018 4:17 PM
  • Please post code using the code posting tool provided. Please format code with proper indenting and spacing.

    You cannot remotely access a file share on a third server due to second hop restriction. Look up "second hop restriction".


    Wednesday, October 10, 2018 4:17 PM
  • Try changing the Fullname property to Directory property:

    Invoke-Command -Computername servername -ScriptBlock {Get-ChildItem -path "\\servername\driveletter\folder1\folder2" -recurse -file *.php |
    Where-Object { $_.LastWriteTime -ge (Get-Date).addminutes(-30) -and ($_.Directory -notin "Directory1","Directory2","Directory3")}  |
    ForEach-Object { $_.FullName } | Out-File -FilePath D:\folder1\Script_results\Files.txt -encoding ASCII }
    Where Directory1, 2 and 3 is the full path the to folders you want to exclude.
    • Edited by F. Van Roie Wednesday, October 10, 2018 4:21 PM
    Wednesday, October 10, 2018 4:18 PM
  • Get-ChildItem -path '\\servername\driveletter\folder1\folder2\*.pdf' -recurse -file | 
            $_.DirectoryName -notmatch 'badfolder1|badfolder2|badfolder3' -and
            $_.LastWriteTime -ge (Get-Date).addminutes(-30)
        select -expand Fullname |
        Out-File -FilePath D:\folder1\Script_results\Files.txt -encoding ASCII


    • Edited by jrv Wednesday, October 10, 2018 4:23 PM
    • Proposed as answer by jrv Wednesday, October 10, 2018 4:29 PM
    Wednesday, October 10, 2018 4:22 PM
  • Thanks for the suggestions all. I will start testing some of them now. Sorry for not formatting my code properly.
    Wednesday, October 10, 2018 5:30 PM
  • I tried out your suggestion but it didn't seem to work. One thing I didn't think about in my original post is that in one of the folders there are subfolders created dynamically that the actual files are in. I did try putting that directory in as such \\servername\drive\directory1\directory2\baddirectory\ and it didnt work. then I tried it as \\servername\drive\directory1\directory2\baddirectory\*\ and that also didnt work. I am not sure what I am doing wrong here so any assistance would be appreciated.
    Friday, October 12, 2018 8:29 PM
  • You have to use this with "match" and "notmatch"


    All special characters have to be escaped.  A "\" escapes a "\".


    Friday, October 12, 2018 8:36 PM
  • Sorry for being unclear my last post related to the code suggested by F. Van Roie. 

    I did try JRV's code this morning but I get the following error when running the script.

    parsing "\\servername\drive\directory1\directory2\baddirectory\*\|\\servername\drive\directory1\directory2\\baddirectory2|\\servername\drive\directory1\directory2\\baddirectory3" - Malformed \p{X} character escape.
    At D:\Scripts\crush_find_test_exclude from technet2.ps1:4 char:9
    +         $_.DirectoryName -notmatch '\\p-w3e-pts55\d$\printclient\toprint\working ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException

    I did try it as you suggested with the "\" escapes, but get the same error.

    Monday, October 15, 2018 1:49 PM
  • You have to escape all special characters.  You cannot use wildcards in a RegEx.


    Monday, October 15, 2018 3:25 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    Best Regards,


    Just do it.

    Friday, October 26, 2018 9:04 AM
  • Hi,

    As this thread has been quiet for a while, we will mark it as ‘Answered’ as the information provided should be helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up. You can also choose to unmark the answer as you wish.

    Best Regards,


    Just do it.

    Monday, November 5, 2018 9:42 AM