Preheat oven to 350°.
Melt butter in a Dutch oven over medium-high heat. Add onion and garlic; sauté 5 minutes or until onion is tender. Combine milk, salt, pepper, and flour, stirring with a whisk. Add milk mixture to pan. Bring to a simmer; cook until slightly thick (about 2 minutes), stirring frequently. Add 4 ounces cheese and ham, stirring until cheese melts. Stir in potatoes.
Place the potato mixture in a 13 x 9-inch baking dish coated with cooking spray. Sprinkle potato mixture with remaining 2 ounces of cheese. Cover with foil coated with cooking spray. Bake at 350° for 45 minutes. Uncover and bake an additional 30 minutes or until lightly browned and potatoes are tender. Let stand 15 minutes before serving.