Replace()

Replace characters within a string.

Syntax
      .Replace(strOldChar, strNewChar)

Key
   strOldChar  The characters to find.

   strNewChar  The characters to replace them with.

Examples

Replace characters in a string:

PS C:\> "abcdef" -replace "dEf","xyz"

Replace characters in a variable:

PS C:\> $demo = "abcdef"
PS C:\> $demo.replace("dEf","xyz")
abcxyz

Rename file extensions from .log to .txt

PS C:\> dir *.log | rename-item $_ -newname { $_.Name -replace "\.log",".txt" }

“In order to be irreplaceable one must always be different” ~ Coco Chanel

Related:

PowerShell Methods



Back to the Top

© Copyright SS64.com 1999-2013
Some rights reserved