I am creating a small Powershell script to overwrite all files in folder with the name by 1 source file. So this is a script:
Version 1
Get-ChildItem -Path 'C:\path\to\folder' -Recurse |
Where-Object { $PSItem.Name -eq 'target-file.ps1' } |
Copy-Item -Path $sourceFile -Destination $PSItem -Force -PassThru
This script error in:
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.
So what is the $PSItem that passed to Copy-Item cmdlet ?
Version 2
Get-ChildItem -Path 'C:\path\to\folder' -Recurse |
Where-Object { $PSItem.Name -eq 'target-file.ps1' } |
Select-Object -ExpandProperty 'FullName' |
Copy-Item -Path $sourceFile -Destination $PSItem -Force -PassThru
So here I extract the property FullName from FileInfo object that run in pipeline just until this line and then it will continue to Copy-Item cmdlet ? No ! Error is:
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.
Version 3
Let's write what's in pipeline at this moment:
Get-ChildItem -Path 'C:\path\to\folder' -Recurse |
Where-Object { $PSItem.Name -eq 'target-file.ps1' } |
Select-Object -ExpandProperty 'FullName' |
Write-Host $PSItem
It is just Write-Host at the end. Result in error:
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.
Version 4
Let's create array:
$allFiles = Get-ChildItem -Path 'C:\path\to\folder' -Recurse |
Where-Object { $PSItem.Name -eq 'target-file.ps1' } |
Select-Object -ExpandProperty 'FullName'
foreach ($item in $allFiles) {
Write-Host $item
}
It is actually output full name to file as expected in the foreach loop.
So - why 3 versions above that did not work and result in error ?
-Pathparameter, just tell it where to copy the piped object to in-DestinationIn examples 1 and two, you are trying to use the piped object itself as destination.. wrong order.. Example 3: just remove| Write-Host $PSItemto see the result on screen. In all examples you don't need the Where-Object clause, just use the-Filterparameter to look for the file with that name-Destination $PSItemsupposed to mean? Are you trying to copy the file to itself?-Pathwhen this parameter is bound from pipeline then you're trying to use-Destination $PSItemwhen$PSItemrepresents the current object in the pipeline