1

Hmm, why doesn't this work? Usually if a parameter takes pipeline input, I can use a script block with it.

PS C:\users\js> (Get-Content -Path file) | Set-Content -Path file -Value { $_ -Replace 1,2 }

Set-Content : 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.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand

Set-Content : 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.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand

Set-Content : 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.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand

This sort of works, but is not the result I want. It takes the script block literally.

PS C:\users\js> (Get-Content -Path file) | Set-Content -Value { $_ -Replace 1,2 }
PS C:\users\js> Get-Content -Path file
 $_ -Replace 1,2

I would expect it to work like this:

PS C:\users\js> Write-Output -InputObject 111,111,111 | Write-Output -InputObject { $_ -Replace 1,2 }

222
222
222
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    Either iterate lines or use the `-raw` parameter with Get-Content and apply the `-replace` directly after the parentheses => `(get-content -path file -raw) -replace 1,2 | set-content -path file` –  May 23 '19 at 21:40
  • The `-Value` parameter and the pipelined input seem to exclude one another with `Set-Content`, the `Write-Output` cmdlet is obviously programmed differently. You can insert it into the pipe, `(Get-Content -Path file)| Write-Output -InputObject { $_ -Replace 1,2 } | Set-Content -Path file` –  May 23 '19 at 21:53

0 Answers0