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.
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.
Don,
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.