Reputation: 5274
I use the following pipeline to select specific folders:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
The above gives me folders that do not have an underscore in their name, which is what I want, and all is good so far.
But when I pipe the result to another Get-ChildItem
, I get a BindingException error:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' } | gci *.pdf
gci : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (Book Folder B:PSObject) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetChildItemCommand
How can I process the files within each folder output from the above pipeline. For example, if a file has a pdf
extension, I would like to invoke the Move-Item
command for it.
Upvotes: 0
Views: 874
Reputation: 30183
The following code snippet outputs fully qualified names of all .pdf
files in subfolders one level under current folder that do not have an underscore in their full path. (Output file names could contain an underscore).
Get-ChildItem -Path . |
Where-Object { $_.PsIsContainer -and $_.Fullname -notmatch '_' } |
Get-ChildItem -Filter *.pdf |
ForEach-Object {
<# do anything with every file object instead of just outputting its FullName <##>
$_.FullName
}
You need to use the -Filter
keyword in the second gci
with respect to its allowed position 2 (note that position 1 is dedicated for -Path
parameter).
For further explanation and for potential improvements/optimalisations, read Get-ChildItem
as well as Get-ChildItem
for FileSystem.
Upvotes: 2
Reputation: 9173
Change the last pipeline object
From gci *.pdf
To Get-childitem -Filter *.pdf
But I would suggest you optimize the total existing line with the below one:
gci -path C:\Folder\Path\* -Filter *.pdf | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
Upvotes: 2