You should take more care about proper code formating, then you would see it. The closing bracket in line 24 is at the wrong place, should be more line 18+
Try
{
$InstanceList = "DESKTOP-6U"
$filepath = "C:\b10\SQLQuery2.sql"
$InstanceList |
ForEach-Object
{
$databases = invoke-sqlcmd -ServerInstance $_ -Database "master" -Query "select name from sys.databases where name like '%i%'"
foreach ($database in $databases)
{
#Execute scripts
Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath
}
}
}
Catch
{
$ErrorMessage = $_.Exception.Message | Out-File C:\b10\DBA_ConfigChanges.txt -append
}