Escalated a few years after the establishment of the Emirates in 1837, the Russian and British interests were in conflict between Muhammad Shah of Iran and Dost Mohammed Khan, which led to the First Anglo-Afghan War which was fought between 1839 and 1842. During the war, Britain occupied the country, in an effort to prevent Afghanistan from coming under Russian control and curb Russian expansion. The war ended with a temporary victory for the United Kingdom, which, however, had to withdraw so that Dost Muhammad came to power again.
Upon the death of Dost Muhammad in 1863, he was succeeded by his son, Sher Ali Khan. However, three years later, his older brother Mohammad Afzal Khan overthrew him. In 1868, Mohammad Afzal Khan was himself overthrown and replaced as Emir by Sher Ali, who returned to the Throne. Sher Ali had spent his few short years in exile in Russia. His return as Emir led to new conflicts with Britain. Subsequently, the British marched on 21 November 1878 into Afghanistan and Emir Sher Ali was forced to flee again to Russia, but he died in 1879 in Mazar-i-Sharif. His successor, Mohammad Yaqub Khan, sought solutions for peace with Russia and gave them a greater say in Afghanistan's foreign policy. However, when the British envoy Sir Louis Cavagnari was killed in Kabul on the 3 September 1879, the British offered to accept Abdur Rahman Khan as Emir. The British concluded a peace treaty with the Afghans in 1880, and withdrew again in 1881 from Afghanistan. The British in 1893 forced Afghanistan to consent to the Durand Line, which is still straight through the settlement area of the Pashtuns runs and about a third of Afghanistan to British India annexing.
After the war, Emir Abdur Rahman Khan, who struck down the country reformed and repressed numerous uprisings. After his death in 1901 his son Habibullah Khan succeeded as emir and continued reforms. Habibullah Khan sought reconciliation with the UK, where he graduated in 1905 with a peace treaty with Russia, stretching for defeat in the Russo-Japanese War had to withdraw from Afghanistan. In the First World War, Afghanistan remained, despite German and Ottoman efforts, neutral (Niedermayer–Hentig Expedition).In 1919 Habibullah Khan was assassinated by political opponents.
Habibullah Khan's son Amanullah Khan was in 1919 against the rightful heir apparent Nasrullah Khan, the then Emir of Afghanistan. Shortly afterwards another war broke which lasted for three months. This war was ended with the Treaty of Rawalpindi after which, the Afghans were able to resume the right to conduct their own foreign affairs as a fully independent state. Amanullah Khan began the reformation of the country and was crowned 1926 Padshah (king) of Afghanistan and founded the Kingdom of Afghanistan.