Tuesday, August 16, 2016

Find and Replace using PowerShell

Below PowerShell will help you to find and replace values in your files.

$rootPath = "C:\myfolder\*.*"
$keyTofind ="UserName"
$keyToReplace  ="Ajeet"
$configFiles = Get-ChildItem -Path $rootPath -Recurse |
Foreach-Object {
    $r = ($_ | Get-Content)
        $r = $r -replace $keyToFind ,$keyToReplace
        [IO.File]::WriteAllText($_.FullName, ($r -join "`r`n"))
}

P.S. Get-Content read contents of file not folder. Please add *.* after your folder path, else you will receive Access dined error.
Use Recurse to go through all folders way under your root folder.

1 comment:

  1. Impressive post..!!!
    I can learn so many interesting or informative post through this blog that are helpful for us...Amazing work you have to do on this blog..Keep sharing much more post...devops openings in hyderabad

    ReplyDelete