Hi Folks
I have a requirement to recursively return certain files (only) from a directory, but I want to explicitly ignore certain parent folders beneath that directory. I then want to export specific detail to CSV
So far I have cobbled together…
$Results = @(Get-ChildItem -Path $Directory -file -Recurse -Exclude "*.txt" ) | Where-Object {<#$_.PSParentPath -notlike "$Directory\Subdir1" -and $_.PSParentPath -notlike "$Directory\Subdir3" -and $_.PSParentPath -notlike $Directory\Subdir12" | Select-Object name,lastwriteTime | Export-Csv "c:\File.csv" -Force
But this doesn’t offer the desired effect. If anyone can point me in the right direction I’d be very grateful
Adam
js2010
February 28, 2019, 5:10am
2
I got it working this way with a bit of tweaking and cleaning up the typos. Have you seen what psparentpath looks like?
Microsoft.PowerShell.Core\FileSystem::/Users/js/foo
I’m using forward slashes in osx.
$Directory = '/Users/js/foo'
Get-ChildItem -path $Directory -file -Recurse -Exclude "*.txt" |
Where-Object { $_.PSParentPath -notlike "*$Directory/Subdir1" -and
$_.PSParentPath -notlike "*$Directory/Subdir3" -and
$_.PSParentPath -notlike "*$Directory/Subdir12" }
Also, you can use the -match comparison operator rather than multiple -notlike. If you are searching for (1) type of file, use -Filter with the file extension.
Get-ChildItem -Path $Directory -Recurse -Filter *.txt |
Where-Object {$_.PSParentPath -notmatch 'Subdir1|Subdir3|Subdir12'}
Thanks for the help here people.
Both solutions worked for me but opted for the -notmatch option just for the typing economy!
js2010
February 28, 2019, 9:50am
5
Here’s an even shorter where-object comparison statement version. You wouldn’t even need the quotes except for the pipe symbol.
Get-ChildItem -Path $Directory -Recurse -Filter *.txt |
Where-Object PSParentPath -notmatch 'Subdir1|Subdir3|Subdir12'