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 ?