'What?' | Where{$_ -replace 'at', 'o'}

I am working on a simple string replace on a config file for a program that I am using while migrating TFS to VSTS, when I notice that Where-Object had a -replace as an option in intellisense. I thought that is strange, let me update help and see what this is supposed to do. It doesn’t show it in there. No problem, let me google it and see what I can come up with. Nothing.

Then I thought, let me just give it a try.

'' | Where{$_ -replace '',''}

(Why does this take the code out from between the quotes?

It did not have any effect on the string. Hmmm.
I thought, I could either look up the source code, or post something here and see if anyone else thought this was insane?

I have a little snip-it picture as proof, in case no one believes this is real. Is there a way to post a little pic?

-replace doesn’t modify a string; it creates a new string. Also, the Where-Object cmdlet is designed to filter items out of the pipeline based on whether they meet the criteria you specified - but -replace doesn’t really do that.

Finally, your -replace criteria simply replaced all blank spaces with more blank spaces.

You probably meant to use ForEach-Object:

‘’ | ForEach-Object { $_ -replace ‘’,‘X’ }

Would output one “X.”

-replace is an operator that you can use anywhere. where just evaluates its expression to true or false.

I’ve just tried this in ISE. Type
‘’ | where {$_ -

A list of operators comes up including replace, split, shl and shr

if you run
Get-Command Where-Object -Syntax
you’ll see that none of those operators are available as parameters on where-object.

I think you’re seeing an intellisense issue. Its showing the full list of PowerShell operators rather than the where-object parameters

if you type
’ ’ | where X -
and then examine the lsit of parameters you’ll see just the ones you expect.

If you use
… | where { }
powershell expects a legal script block as the filter usually of the form
{$_.property }

if you use the newer syntax it expects
… | where
the operators are actually parameters on where-object rather than the PowerShell operators.

You’ve applied the newer style documentation to the older style code which is where the confusion arises.
Hope this helps

Thanks Richard, It is nice to see that I am not the only crazy one. I was thinking, who knows, now that this is open source maybe someone came up with a clever reason to put a -replace in a filtering cmdlets…lol

I was working on a 2016 Windows Server, I also thought that there might be something new and crazy going on there.

It may have done this before in the ISE and I just never noticed, but I highly doubt it. I live in the ISE all day every day. I usually have 2 or 3 open with 4 to 6 tabs each.

Like the example in the title, I had strings in my code example between the quotes, but when I posted it, all of the strings were cleared out and it just left the quotes. I tried it twice, and it did the same thing both times.

It’s a script block. You can put anything you want in there. Here’s a bad example:

PS C:\> ps | where { $_.processname -replace 'host','horse' -eq 'svchorse' }

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    263     103     2900       3932 ...99             476 svchost
    783      30    14144      13016 ...45             616 svchost
    614      22     7388      10448 ...20             728 svchost
    561      20     4736       5952 ...34             784 svchost
   1020      56    11500      13120 ...64             924 svchost
    610      30     8076       8984 ...99             932 svchost
    526      32     6856       9428 ...47            1016 svchost
   1665      61    17216      25396 ...58            1096 svchost
    499      44    15248      16332 ...80            1872 svchost
    451      23     6888      11724 ...95            2012 svchost
    363      16     4616       8304 ...61            2280 svchost
    114       9     1272       2492 ...77            3848 svchost
    171      12     2156      11300 ...23     0.03   5320 svchost

What string was between the quotes?