Powershell, using contains to check if files contain a certain word

后端 未结 3 1246
醉话见心
醉话见心 2021-01-12 12:44

I am trying to create a powershell script which looks through all files and folders under a given directory it then changes all instances of a given word in .properties file

3条回答
  •  长发绾君心
    2021-01-12 12:52

    Simplified contains clause

    $file = Get-Content $_.FullName
    
    if ((Get-Content $file | %{$_ -match $wordToFind}) -contains $true) {
        Add-Content log.txt $_.FullName
        ($file) | ForEach-Object { $_ -replace $wordToFind , $wordToReplace } | 
        Set-Content $_.FullName
    }
    

提交回复
热议问题