I wanted to make a powershell script to make altering a line in multiple files go faster.
It's really the first time I'm trying to make something like this. What I basically want to do :
- Select all the files in a directory that have a ".xml" extension
- Go through every line per file and check if the it contains de following text (example:
<package>true</package>
) / - Then I want to change the above example text to the new text (example:
<package>false</package>
) Also if the file doesn't contain<package>true</package>
I also want to add<package>false</package>
- After that it should just save the file not create a new one somewhere else.
I've never really used powershell before so I did some digging and came up with this code:
$files = Get-ChildItem –Path 'C:\test-XML\XML' -Recurse -Filter *.xml
$tempFilePath = 'C:\test-XML\newXml'
$find = '<packag>true</package>'
$replace = '<packag>false</package>'
foreach ($f in $files){
(Get-Content $f) -replace $find, $replace | Add-Content -Path $tempFilePath
}
With this code I should be able to get all the files in the directory and then loop trough it and change the text with the newer text. But it doesn't work and it also doesn't cover all the points I needed to make the task easier.
Does anyone have insights to make it work and or even make it better than it is now.