This PowerShell script is a simple find/replace tool, much like the ones you find inside your favourite text editors. Unlike most text editors, however, this will run against all files of a certain extension within a specified folder. This enables you to edit, say for example, every CSV file in folder C:\LotsofCSVfiles.
I wrote this script as I had a large number of CSV files that I needed the same find/replace function performed on all files. Rather than opening 100+ CSV files and running a find/replace one-by-one, I could simply tell this script to search for all files in the folder and do the find/replace.
By default, the script looks for CSV files, but using the -Ext parameter, you can select a different file extension. As the standard functions work via regular expressions, I have also added a line to escape any characters to prevent failures.
Usage: .\FindReplace.ps1 -Path C:\folder\path -String “findme” -Replace “replaceme” [-Ext txt]
Download the Find/Replace script v1.0 here!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
################################################################################################# # # # Find/Replace Script v1.0 # # # # Written by: Mike Oldfield # # Date: 18/05/2016 # # # # This PS1 is a simple find/replace which will grab all files of a specified extension and # # run the replace against each file. This is good for when you need to perform the same # # find/replace function across a large number of files # # # # Usage: .\FindReplace.ps1 -Path C:\folder\path -String "findme" -Replace "replaceme" # # [-Ext csv] # # # ################################################################################################# ############################# # # # PARAMETERS # # # ############################# # Set some avaialble paramters. This bit must come first param( [string]$Path, # -Path: Where is the folder full of files we're looking at? [string]$String, # -String: Specify the string we're looking for [string]$Replace, # -Replace: Specify the string you want to replace $String with when found [string]$Ext = "csv" # -Ext: Specify the extension of the files you're after. Default: CSV ) $scriptVer = "v1.0" # What version of the script is this? Used for header info $scriptAuth = "Mike Oldfield" # Who wrote the script? $scriptLastUpdate = "31/05/2016" # When was the script last updated? ############################# # # # HEADER # # # ############################# # Write a pretty header Write-Host "`r`n##################################################`r`n" -ForegroundColor darkcyan -NoNewLine Write-Host "##### " -ForegroundColor darkgray -NoNewLine Write-Host "Find/Replace Script $scriptVer " -ForegroundColor gray -NoNewLine Write-Host "#####`r`n##### " -ForegroundColor darkgray -NoNewLine Write-Host "Written by: $scriptAuth " -ForegroundColor gray -NoNewLine Write-Host " #####`r`n##### " -ForegroundColor darkgray -NoNewLine Write-Host "Last Updated: $scriptLastUpdate " -ForegroundColor gray -NoNewLine Write-Host " #####`r`n" -ForegroundColor darkgray -NoNewLine Write-Host "##################################################`r`n" -ForegroundColor darkcyan ############################# # # # SCRIPTY BIT # # # ############################# # Find out what file extension we asked for, and add *. before it as a wildcard for all files $Ext = "*.$Ext" # Take the $Path we specified and find all the files of extension $Ext. Just take the name from the results $CSVList = Get-ChildItem "$Path" -Force -Filter $Ext -ErrorAction Stop | select name # Count how many objects we found in the last request $CSVCount = $CSVList | measure # Tell us what we're looking for Write-Host "Original string to search: " -ForegroundColor yellow -NoNewLine Write-Host $string # Tell us what we're going to replace it with Write-Host "Replace string with: " -ForegroundColor yellow -NoNewLine Write-Host $replace Write-Host "----------------------------------------------`r`n" -ForegroundColor darkgray # Now that we've printed the pretty version, do some RegEx escaping # This adds some extra characters to escape things like \ # It'll make D:\Global into D:\\Global for example # Stops it failing on silly things! $string = [Regex]::Escape($string) # Now for each of the files we found... foreach ($CSV in $CSVList) { # Tell us what file we're about to edit... Write-Host "Replacing strings in $($CSV.Name) ... " -NoNewLine # Get the content of the file $CSVContent = Get-Content $Path\$($CSV.Name) # Do your find/replace magic! $CSVContent = $CSVContent -replace $string, $replace # Write the content back to the original file #$CSVContent | Out-File $Path\$($CSV.Name) -Encoding UTF8 $FinalPath = "$Path\$($CSV.Name)" $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False) [System.IO.File]::WriteAllLines($FinalPath, $CSVContent)#, $Utf8NoBomEncoding) # Tell us you're done! Write-Host "OK!" -ForegroundColor green } # Tell us how many files we edited Write-Host "`r`nTotal Files: "-ForegroundColor yellow -NoNewLine Write-Host $CSVCount.Count "`r`n" |