$FileContent isn’t a string - it’s a collection of strings, with one object for each line in the file. You can’t readily execute comparison operators against a collection. It’s like taking a bunch of cars and asking, “is this red?” What you’ll probably need to do is use a ForEach loop to enumerate each line in the file:
ForEach ($line in $FileContent) {
}
Further, the -contains operator isn’t a string comparison operator. I suspect you’re looking for -ilike instead. Read about_comparison_operators in PowerShell (it’s a help file).
Additionally, Set-Content isn’t necessarily the right command for outputting. It’s intended to set the entire content of the file, and you’re going to end up giving it one line at a time. Look into Out-File and the -Append parameter, instead, so you can output one line at a time.
Additionally you are running Get-ChildItem -recurse within the foreach block which will give you a collection of FileInfo objects. Then you pass it to get-content which ultimately creates an amalgamation of all the files rather than individual files.
Try running get-childitem outside of the foreach and using a nested foreach to cycle through each line as Don suggested.
# Add filters to Get-ChildItem as necessary
Get-ChildItem -Recurse | ForEach-Object {
$FileContent = Get-Content....
ForEach ($line in $FileContent) {
...
}
}
Although I don’t think it a good option because it will remove any good formatting if the code is well structured. You could type cast your content as a string and do a complete replace or use -join to make it a single string. I agree with Don and Chris however that looping through the content would be best.