Write to the output between two pipeline












1















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
    Nov 19 '18 at 10:02











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

    – Zou
    Nov 19 '18 at 10:28











  • 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
    Nov 19 '18 at 10:34


















1















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
    Nov 19 '18 at 10:02











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

    – Zou
    Nov 19 '18 at 10:28











  • 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
    Nov 19 '18 at 10:34
















1












1








1








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 Nov 19 '18 at 9:54









ZouZou

687




687













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

    – marsze
    Nov 19 '18 at 10:02











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

    – Zou
    Nov 19 '18 at 10:28











  • 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
    Nov 19 '18 at 10:34





















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

    – marsze
    Nov 19 '18 at 10:02











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

    – Zou
    Nov 19 '18 at 10:28











  • 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
    Nov 19 '18 at 10:34



















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

– marsze
Nov 19 '18 at 10:02





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

– marsze
Nov 19 '18 at 10:02













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

– Zou
Nov 19 '18 at 10:28





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

– Zou
Nov 19 '18 at 10:28













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
Nov 19 '18 at 10:34







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
Nov 19 '18 at 10:34














1 Answer
1






active

oldest

votes


















2














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
    Nov 19 '18 at 16:08











  • @Zou That's only partly true. I've updated my answer to be more clear.

    – marsze
    Nov 20 '18 at 7:45











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',
autoActivateHeartbeat: false,
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









2














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
    Nov 19 '18 at 16:08











  • @Zou That's only partly true. I've updated my answer to be more clear.

    – marsze
    Nov 20 '18 at 7:45
















2














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
    Nov 19 '18 at 16:08











  • @Zou That's only partly true. I've updated my answer to be more clear.

    – marsze
    Nov 20 '18 at 7:45














2












2








2







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 Nov 20 '18 at 7:44

























answered Nov 19 '18 at 11:00









marszemarsze

5,12331841




5,12331841













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

    – Zou
    Nov 19 '18 at 16:08











  • @Zou That's only partly true. I've updated my answer to be more clear.

    – marsze
    Nov 20 '18 at 7:45



















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

    – Zou
    Nov 19 '18 at 16:08











  • @Zou That's only partly true. I've updated my answer to be more clear.

    – marsze
    Nov 20 '18 at 7:45

















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

– Zou
Nov 19 '18 at 16:08





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

– Zou
Nov 19 '18 at 16:08













@Zou That's only partly true. I've updated my answer to be more clear.

– marsze
Nov 20 '18 at 7:45





@Zou That's only partly true. I've updated my answer to be more clear.

– marsze
Nov 20 '18 at 7:45




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents