Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between .replace and -replace in powershell?

I was under impression that .replace and -replace were the exact same thing, however I found that I could not accomplish some RegEx tasks with .replace that I could with -replace. Could someone please point out what I'm missing?

Broken Regex replace:
$a=$a.Replace('.:\\LOGROOT\\', "\\$env:computername\logroot\")


Working Regex replace:
$a=$a -Replace('.:\\LOGROOT\\', "\\$env:computername\logroot\")

ps: The following URL leads me to think there are .replace options I am unfamiliar with, but I cant seem to find any additional information on how to use them, or how to access the help for these options. http://www.computerperformance.co.uk/powershell/powershell_regex.htm Regex.Replace(String, String, String, RegexOptions) and also: Regex.Replace(String, String, MatchEvaluator, RegexOptions) methods.

Thank you

like image 305
mr.buttons Avatar asked Apr 17 '12 02:04

mr.buttons


People also ask

What does :: mean in PowerShell?

Static member operator :: To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. The member name may be an expression. PowerShell Copy.

How do I find the hostname of a remote computer using PowerShell?

The hostname command couldn't be any simpler. Open up a PowerShell (or even cmd .exe prompt) and type hostname . Done. This command returns a single string (the computer name of the local computer).

How do I echo in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.

How do I change the hostname in PowerShell?

Type Start PowerShell and press Enter in the Command Prompt window to start Windows PowerShell. 2. Type Rename-Computer -NewName CN1 -DomainCredential vdomain\Administrator -PassThru and press Enter to change the computer name. Replace CN1 with the name of the computer and vdomain with the domain name.


2 Answers

While @Keith Hill's answer explains the difference between Replace method and the -replace operator, to explain why you might not see the same result, it is because you are using the String.Replace method which does string replace and -replace operator uses regex replace. You can use the Regex.Replace method for this purpose and you should see the same effect:

[regex]::replace($a,'.:\\LOGROOT\\', "\\$env:computername\logroot\") 

In short, the -replace operator is same as Regex.Replace (the particular overload linked above), but in general Replace() can be instance or static method that can be doing anything completely different from -replace

like image 80
manojlds Avatar answered Sep 21 '22 19:09

manojlds


They are not the same thing. .Replace is a .NET method either on System.String or any other type with an instance method named Replace. -replace is a PowerShell operator that that uses regular expressions. Run man about_operators to see more info on the -replace operator.

like image 24
Keith Hill Avatar answered Sep 22 '22 19:09

Keith Hill