Write to the output between two pipeline











up vote
1
down vote

favorite












I'm trying to write to the output (echo) within two pipe to have a trace of what I'm doing but I have an error.



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Write-Output "deleting file" -PassThru |
Remove-Item


but I have this error :




Write-Output : 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.




I also tested in the Foreach-Object function



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Foreach-Object {
echo "deleting $($_.Name)";
Remove-Item($_);
}


but here nothing is printed in the console (but the script finish without error and deleted the files I wanted)



So how can I print to thje output wihtin two Pipe ?



Thx










share|improve this question






















  • That probably means the condition in your where is not true for any file?
    – marsze
    yesterday










  • @marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
    – Zou
    yesterday










  • In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
    – LotPings
    yesterday

















up vote
1
down vote

favorite












I'm trying to write to the output (echo) within two pipe to have a trace of what I'm doing but I have an error.



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Write-Output "deleting file" -PassThru |
Remove-Item


but I have this error :




Write-Output : 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.




I also tested in the Foreach-Object function



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Foreach-Object {
echo "deleting $($_.Name)";
Remove-Item($_);
}


but here nothing is printed in the console (but the script finish without error and deleted the files I wanted)



So how can I print to thje output wihtin two Pipe ?



Thx










share|improve this question






















  • That probably means the condition in your where is not true for any file?
    – marsze
    yesterday










  • @marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
    – Zou
    yesterday










  • In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
    – LotPings
    yesterday















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to write to the output (echo) within two pipe to have a trace of what I'm doing but I have an error.



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Write-Output "deleting file" -PassThru |
Remove-Item


but I have this error :




Write-Output : 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.




I also tested in the Foreach-Object function



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Foreach-Object {
echo "deleting $($_.Name)";
Remove-Item($_);
}


but here nothing is printed in the console (but the script finish without error and deleted the files I wanted)



So how can I print to thje output wihtin two Pipe ?



Thx










share|improve this question













I'm trying to write to the output (echo) within two pipe to have a trace of what I'm doing but I have an error.



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Write-Output "deleting file" -PassThru |
Remove-Item


but I have this error :




Write-Output : 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.




I also tested in the Foreach-Object function



Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Foreach-Object {
echo "deleting $($_.Name)";
Remove-Item($_);
}


but here nothing is printed in the console (but the script finish without error and deleted the files I wanted)



So how can I print to thje output wihtin two Pipe ?



Thx







powershell pipe echo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Zou

487




487












  • That probably means the condition in your where is not true for any file?
    – marsze
    yesterday










  • @marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
    – Zou
    yesterday










  • In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
    – LotPings
    yesterday




















  • That probably means the condition in your where is not true for any file?
    – marsze
    yesterday










  • @marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
    – Zou
    yesterday










  • In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
    – LotPings
    yesterday


















That probably means the condition in your where is not true for any file?
– marsze
yesterday




That probably means the condition in your where is not true for any file?
– marsze
yesterday












@marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
– Zou
yesterday




@marsze the condition is correct.The script works well when I remove the "Write-Output pipe"
– Zou
yesterday












In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
– LotPings
yesterday






In the 2nd script either use $_ | Remove-Item or in the 1st insert | Tee-Object -Variable Deleted | instead of Write-Output and later output $Deleted
– LotPings
yesterday














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:



Write-Output "Test"


or this:



"Test" | Write-Output


but not this:



"Test" | Write-Output "Test"


Solution:



Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}


Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!



In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):



(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }


So it's always safest to use $_.FullName.






share|improve this answer























  • thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
    – Zou
    yesterday










  • @Zou That's only partly true. I've updated my answer to be more clear.
    – marsze
    22 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372090%2fwrite-to-the-output-between-two-pipeline%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:



Write-Output "Test"


or this:



"Test" | Write-Output


but not this:



"Test" | Write-Output "Test"


Solution:



Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}


Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!



In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):



(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }


So it's always safest to use $_.FullName.






share|improve this answer























  • thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
    – Zou
    yesterday










  • @Zou That's only partly true. I've updated my answer to be more clear.
    – marsze
    22 hours ago















up vote
2
down vote



accepted










Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:



Write-Output "Test"


or this:



"Test" | Write-Output


but not this:



"Test" | Write-Output "Test"


Solution:



Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}


Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!



In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):



(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }


So it's always safest to use $_.FullName.






share|improve this answer























  • thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
    – Zou
    yesterday










  • @Zou That's only partly true. I've updated my answer to be more clear.
    – marsze
    22 hours ago













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:



Write-Output "Test"


or this:



"Test" | Write-Output


but not this:



"Test" | Write-Output "Test"


Solution:



Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}


Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!



In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):



(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }


So it's always safest to use $_.FullName.






share|improve this answer














Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:



Write-Output "Test"


or this:



"Test" | Write-Output


but not this:



"Test" | Write-Output "Test"


Solution:



Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?.log.(d{4})-(d{2})-(d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}


Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!



In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):



(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }


So it's always safest to use $_.FullName.







share|improve this answer














share|improve this answer



share|improve this answer








edited 22 hours ago

























answered yesterday









marsze

4,13131640




4,13131640












  • thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
    – Zou
    yesterday










  • @Zou That's only partly true. I've updated my answer to be more clear.
    – marsze
    22 hours ago


















  • thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
    – Zou
    yesterday










  • @Zou That's only partly true. I've updated my answer to be more clear.
    – marsze
    22 hours ago
















thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
– Zou
yesterday




thx for your reply. About Remove-Item, apparently it can deal with $_ alone. No need to specify the fullName.
– Zou
yesterday












@Zou That's only partly true. I've updated my answer to be more clear.
– marsze
22 hours ago




@Zou That's only partly true. I've updated my answer to be more clear.
– marsze
22 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372090%2fwrite-to-the-output-between-two-pipeline%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Saint-Aignan (Tarn-et-Garonne)

Volksrepublik China

How to test boost logger output in unit testing?