Once in the service of the Habsburgs, he distinguished himself in the Battle of Villaviciosa and in the thankless task of evacuation of Aragon in 1711. In recognition of his accomplishments, he was appointed supreme commander of the forces of Catalonia and was charged with organizing city defenses in the 1713-14 Siege of Barcelona. With just over 5,000 men, of whom about 3,500 were members of the guild militia, Villarroel was forced to defend the city against some 40,000 mixed French and Spanish supporters of Phillip V. As the summer of 1714 passed, desperate times in Barcelona forced Villarroel to attempt to force an exit of the city. The attempted exit was repulsed by the Duke of Berwick who had replaced the Duke of Popoli as commander of the besiegers. By September, the Franco-Spanish forces had opened a sizable breach in the walls and the city counselors pleaded with Villarroel to capitulate the city to the Duke of Berwick. Refusing to surrender the city, Villarroel continued defending the city until he was injured, whereupon the city was officially surrendered to the forces of Phillip V. Despite assurances given contrary to the fact, the 25 military commanders of Barcelona's defenses, including Villarroel, were all imprisoned.