logo-powershellChange a string in a text file

This script lists all files in a specific folder and replace a string within these files

The following cmdlets are used in the script :

  • Get-childitem : list the files to update
  • Get-content : open and read the files
  • Set-content : write the changes to file

Script :


Replace method


Replace characters within a string.


strOldChar The characters to find.

strNewChar The characters to replace them with.

Replace characters in a string:

Replace characters in a variable:

Multiple replacements can be chained together in one command:

Search and Replace characters in a file:

Rename file extensions from .log to .txt

Using single quotes around the search strings will ensure that all punctuation is ignored by PowerShell.

An alternative method to read an entire file as a single long text string is the .Net ::ReadAllText method:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social Media Auto Publish Powered By : XYZScripts.com
%d bloggers like this: