Preheat the oven to 400 degrees F. Coat a 3 quart baking dish with cooking spray.
Melt the butter in a large pan over medium heat. Add the onions and cook for 4-5 minutes or until softened.
Add the garlic and cook for 30 seconds, stirring constantly.
Stir in the flour and cook for 1 minute.
Gradually add the milk to the pan, whisking after each addition. Add the salt and pepper, then bring the sauce to a simmer.
Cook for 2-3 minutes or until just thickened. Remove from the heat and stir in 1 cup of cheese.
Place half of the potatoes in the bottom of the pan. Top with half of the ham. Pour half of the sauce over the top, then sprinkle with 1 cup of cheese.
Repeat the process with the remaining potatoes, ham, sauce and cheese.
Cover the pan with foil and bake for 50 minutes.
Uncover and bake for an additional 20 minutes or until top is browned and potatoes are tender. If the top browns before the potatoes are cooked through, you can cover it back up with foil.
Let stand for 5-7 minutes. Sprinkle with parsley, then serve.