Start by peeling and dicing the potatoes into even cubes so they cook uniformly. Boil them in salted water until just tender but not falling apart. Drain and set aside, then chop your cooked ham into bite-sized pieces.
In a saucepan, melt butter over medium heat and sauté onions until soft. Add garlic, then whisk in flour to form a light roux. Slowly pour in milk while whisking until smooth and thick, then stir in shredded cheese until melted.
In a large bowl, gently mix cooked potatoes, ham, and cheese sauce until everything is evenly coated. Be careful not to mash the potatoes too much so they keep their texture in the casserole.
Pour the mixture into a greased baking dish and spread it evenly. Sprinkle extra cheese on top for a golden, bubbly crust. This layer becomes beautifully crisp while baking.
Bake in a preheated oven at 375°F (190°C) for about 40–45 minutes. Once the top is golden and the edges are bubbling, remove it and let it rest before serving.