Home > Software design >  How to write multiple function calls in multiple lines in PowerShell?
How to write multiple function calls in multiple lines in PowerShell?

Time:11-03

I'm currently playing around on PowerShell, and I'm wondering how I can call multiple functions after eachother, but giving them each their own line.

I currently have this:

$leet= $text.replace("a", "4").replace("e", "1").replace("e", "3");

But I want it more like this:

$leet= $text
    .replace("a", "4")
    .replace("e", "1")
    .replace("e", "3");

But PowerShell doesn't really like the newlines, and it doesn't work either when I add the ` to the end of each line followed by another one.

So, am I missing something, or is this not possible in PowerShell?

CodePudding user response:

As Jeroen Mostert notes you can get line continuation for free by specifying the operator (.) and then placing the whitespace between it and the right-hand operand:

$leet = $text.
    Replace("a", "4").
    Replace("e", "1").
    Replace("e", "3")

(note that the last call, Replace("e", "3"), does nothing - all the e's have already been replaced by the preceding call)

  • Related