'Powershell Script to update a line in a text file based off a get-childitem recursive search

Have an ini file I need to update the contents of. There is a line within that contains the path to javaw.exe. As part of an automated build, the path changes as Java versions update.

Need to overwrite the current line with the output of $directory

Sample of $directory - C:\Program Files\Eclipse Adoptium\jdk-17.0.1.12-hotspot\bin\javaw.exe

This is what I have so far, but the file is not updated, no errors.

Tried hardcoding the full path in $find but no joy, it does need to be dynamic though as I don't want to maintain a static version number, any ideas?

$filename = 'javaw.exe'
$searchinfolder = 'C:\Program Files\Eclipse Adoptium'
$directory = Get-ChildItem -Path $searchinfolder -Filter $filename -Recurse | %{$_.FullName}
$file = 'C:\Program Files (x86)\Eclipse\eclipse.ini'
$find = 'C:\Program Files\Eclipse Adoptium\'
(Get-Content $file.replace($find,$directory)) | Set-Content $file


Solution 1:[1]

Theo got me on the right track.

Final code that did what I wanted

$filename = 'javaw.exe'
$searchinfolder = 'C:\Program Files\Eclipse Adoptium'
$directory = Get-ChildItem -Path $searchinfolder -Filter $filename -Recurse | %{$_.FullName}
$file = 'C:\Program Files (x86)\Eclipse\eclipse.ini'
$find = (Get-Content -Path $file -TotalCount 2)[-1]
(Get-Content $file -raw).replace($find,$directory) | Set-Content $file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NitroMatt