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
powershell pipe echo
add a comment |
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
powershell pipe echo
That probably means the condition in yourwhere
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
add a comment |
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
powershell pipe echo
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
powershell pipe echo
asked yesterday
Zou
487
487
That probably means the condition in yourwhere
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
add a comment |
That probably means the condition in yourwhere
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
add a comment |
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
.
thx for your reply. AboutRemove-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
add a comment |
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
.
thx for your reply. AboutRemove-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
add a comment |
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
.
thx for your reply. AboutRemove-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
add a comment |
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
.
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
.
edited 22 hours ago
answered yesterday
marsze
4,13131640
4,13131640
thx for your reply. AboutRemove-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
add a comment |
thx for your reply. AboutRemove-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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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