Preheat your oven to 375°F.
Grease the baking dish with butter or cooking spray.
In a saucepan, melt butter over medium heat.
Whisk in flour to make a smooth roux.
Slowly add milk while whisking continuously.
Cook until thickened, about 5 minutes.
Stir in cheese, salt, pepper, and garlic powder.
Add ham, potatoes, and onions into the dish.
Pour the cheese sauce over the mixture.
Stir lightly to ensure even coating.
Cover dish with foil and bake for 40 minutes.
Remove foil and bake for another 15 minutes.
Let it rest 10 minutes before serving.