Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetch all the files in a folder but exclude all the files in a sub folder by using powershell

I have a powershell script to fetch all the files in a folder but exclude all the files in a sub folder. I am using the following script, but not giving the expected results.

Get-ChildItem -Path $srcpath -Force -Exclude "Monthly Reports" -Recurse

Note: Trying to Exclude the files in the sub folder "Monthly Reports"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please suggest me on this. Thanks In Advance.

>Solution :

-Exclude preforms exclusions based on the item’s .Name property, and what you need in this case is to exclude them based on their .FullName property, a layer of filtering on that property is required:

Get-ChildItem -Path $srcpath -Force -Recurse |
    Where-Object FullName -NotMatch '[\\/]Monthly Reports[\\/]?'

If you want to exclude all childs of that folder but not the folder itself you can change the pattern to:

Get-ChildItem -Path $srcpath -Force -Recurse |
    Where-Object FullName -NotMatch '[\\/]Monthly Reports[\\/].'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading