The history of the county of Portugal is traditionally dated from the reconquest of Portus Cale (Porto) by Vímara Peres in 868. He was named a count and given control of the frontier region between the Limia and Douro rivers by Alfonso III of Asturias. South of the Douro, another border county would be formed decades later when what would become the County of Coimbra was conquered from the Moors by Hermenegildo Gutiérrez. This moved the frontier away from the southern bounds of the county of Portugal, but it was still subject to repeated campaigns from the Caliphate of Cordoba. The recapture of Coimbra by Almanzor in 987 again placed the County of Portugal on the southern frontier of the Leonese state for most of the rest of the first county's existence. The regions to its south were only again conquered in the reign of Ferdinand I of León and Castile, with Lamego falling in 1057, Viseu in 1058 and finally Coimbra in 1064.
The leaders of the first county of Portugal reached the height of their power in the late 10th century, when Count Gonzalo Menéndez may have used the title magnus dux portucalensium ("grand duke of Portugal") and his son Menendo used the title dux magnus (grand duke). It could have been this Count Gonzalo who assassinated Sancho I of León after inviting the King to a banquet and offering him a poisoned apple. Not all historians, however, believe that Gonzalo Menéndez was responsible for the king's death and some attribute the regicide to a contemporary count named Gonzalo Muñoz.
In the late 960s Gonzalo's lands were ravaged by Vikings, and in 968, he fell out with king Ramiro III over the latter's refusal to fight the raiders. His son Menendo had close relations with Ramiro's rival and successor, Bermudo II, being made the king's alférez and tutor of his son, the future king Alfonso V. Following Alfonso's succession, Menendo would serve as regent for the boy king and marry him to one of Menendo's daughters.
The county continued with varying degrees of autonomy within the Kingdom of León and, during brief periods of division, the Kingdom of Galicia until 1071, when Count Nuno Mendes, desiring greater autonomy for Portugal, was defeated and killed in the Battle of Pedroso by King García II of Galicia, who then proclaimed himself the King of Galicia and Portugal, the first time a royal title was used in reference to Portugal. The independent county was abolished, its territories remaining within the crown of Galicia, which was in turn subsumed within the larger kingdoms of García's brothers, Sancho II and Alfonso VI of León and Castile.