Search code examples
windowspowershelllowercase

script to convert all .txt file content to lowercase


I have about 700 .txt files scattered in 300 directories and sub-directories.

I would like to open each of them, convert all text inside to lowercase, including Unicode characters (such as É to é), then save and close them.

Can you advise how it could be done through PowerShell? It is my own computer and I have admin rights.

I have started with the below:

Get-ChildItem C:\tmp -Recurse -File | ForEach-Object {}

but I am not sure what to put between the brackets of ForEach-Object {}.


Solution

  • Simple script, with your requirements:

    $path=".\test\*.txt"
    #With Default system encoding
    Get-ChildItem $path -Recurse | foreach{    
        (Get-Content $_.FullName).ToLower() | Out-File $_.FullName
    }
    
    #Or with specified encoding    
    Get-ChildItem $path -Recurse | foreach{    
    (Get-Content $_.FullName -Encoding Unicode).ToLower() | 
        Out-File $_.FullName -Encoding Unicode
    }
    #Test
    Get-ChildItem $path -Recurse | foreach{
        Write-Host "`n File ($_.FullName): `n" -ForegroundColor DarkGreen
        Get-Content $_.FullName    
    }