Posted On December 9, 2020

PowerShell: Replacing Characters Inside Text Files

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Replacing Characters Inside Text Files
$textFilesDirectory="C:\Users\$env:computername\Desktop\test"
$textFileExtensions='.txt'
$regexNewlineOnly="(?<!\r)\n$"
$replaceWith="`r`n"

function updateFile{
    param(
        $file="C:\Users\$env:username\Desktop\test.txt",
        $regexMatch="(?<!\r)\n$",
        $replaceWith="`r`n",
        $output
    )
    $output=if($output){$output}else{$file}
    if($file -ne $output){
        write-host "Processing $file`r`n=> updating $output"
    }else{
        write-host "Processing $file"
    }
    $content=get-content $file  # This avoids the default the get-content as an array of strings
    $newContent=$content|%{$_.Replace($regexMatch,$replaceWith)}
    [System.IO.File]::WriteAllText($output,$($newContent|out-string)) # peruse the dotnet framework, which is independent of PoSh versions

    # This alternate method is to create an ISO-8859-1 encoded file.
    # It's not fully compatible with the most widely used legacy Windows code page, Windows-1252
    # The solution will not work with characters not valid for ISO-8859-1,
    # such as €: The following command breaks, for instance: Set-Content test.txt ([byte[]][char[]] "€") -Encoding Byte
    # Set-Content $file -Force ([byte[]][char[]] $newContent) -Encoding Byte
    #
    # the -nonewline directive only works with PoSH v3+
    # $newContent|out-string|Set-Content -NoNewline $output -force
}

# One of the fastest methods to obtain list of files inside a directory
$files=(get-childitem $textFilesDirectory -recurse | where {$_.extension -in $textFileExtensions}).FullName
$files|%{updateFile $_ $regexNewlineOnly $replaceWith}

Leave a Reply

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

Related Post

PowerShell: Microsoft Dynamics Update All Organizations

# updateCrmOrgs.ps1 function updateCrmOrgs{ try{ Add-PSSnapin Microsoft.Crm.Powershell $servers=Get-CrmServer $highestVersion=($servers.Version|measure-object -Maximum).Maximum $lowVersionServers=$servers|?{[version]$_.Version -lt [version]$highestVersion} if($lowVersionServers){ write-warning…

Linux: How To Install Visual Studio Code

Intro Bypassing any discussions about choosing Visual Studio Code (VS Code) over Atom or Sublime…

PowerShell: How To Install VMM Agent on Hyper-V Nodes

# Update these variables to match your system $appName='Microsoft System Center Virtual Machine Manager Agent…