quality:correction
Barakzai dynasty and British wars

By the early 19th century the Afghan empire was under threat from the Persians in the west and the Sikh Empire in the east. Fateh Khan, leader of the Barakzai tribe, installed many of his brothers in positions of power throughout the empire. Fateh Khan was brutally murdered in 1818 by Mahmud Shah. As a result, the brothers of Fateh Khan and the Barakzai tribe rebelled, and a civil war brewed. During this turbulent period, Afghanistan fractured into many states, including the Principality of Qandahar, Emirate of Herat, Khanate of Qunduz, Maimana Khanate, and numerous other warring polities. The most prominent state was the Emirate of Kabul, ruled by Dost Mohammad Khan.

With the collapse of the Durrani Empire, and the exile of the Sadozai Dynasty to be left to rule in Herat, Punjab and Kashmir were lost to Ranjit Singh, ruler of the Sikh Empire, who invaded Khyber Pakhtunkhwa in March 1823 and captured the city of Peshawar following the Battle of Nowshera. In 1834, Dost Mohammad Khan led numerous campaigns, firstly campaigning to Jalalabad, and then allying with his rival brothers in Kandahar to defeat Shah Shuja Durrani and the British in the Expedition of Shuja ul-Mulk. In 1837, Dost Mohammad Khan attempted to conquer Peshawar and sent a large force under his son Wazir Akbar Khan, leading to the Battle of Jamrud. Akbar Khan and the Afghan army failed to capture the Jamrud Fort from the Sikh Khalsa Army, but killed Sikh Commander Hari Singh Nalwa, thus ending the Afghan-Sikh Wars. By this time the British were advancing from the east, capitalizing off of the decline of the Sikh Empire after it had its own period of turbulence following the death of Ranjit Singh, which engaged the Emirate of Kabul in the first major conflict during " The Great Game ".

In 1839 a British expeditionary force marched into Afghanistan, invading the Principality of Qandahar, and in August 1839, seized Kabul. Dost Mohammad Khan defeated the British in the Parwan campaign, but surrendered following his victory. He was replaced with the former Durrani ruler Shah Shuja Durrani as the new ruler of Kabul, a de facto puppet of the British. Following an uprising that saw the assassination of Shah Shuja, the 1842 retreat from Kabul of British-Indian forces and the annihilation of Elphinstone 's army, and the punitive expedition of The Battle of Kabul that led to its sacking, the British gave up on their attempts to try and subjugate Afghanistan, allowing Dost Mohammad Khan to return as ruler. Following this, Dost Mohammad pursued a myriad of campaigns to unite most of Afghanistan in his reign, launching numerous incursions including against the surrounding states such as the Hazarajat campaign, conquest of Balkh, conquest of Kunduz, and the conquest of Kandahar. Dost Mohammad led his final campaign against Herat, conquering it and re-uniting Afghanistan. During his campaigns of re-unification, he held friendly relations with the British despite the First Anglo-Afghan War, and affirmed their status in the Second Anglo-Afghan treaty of 1857, while Bukhara and internal religious leaders pressured Dost Mohammad to invade India during the Indian Rebellion of 1857.

Dost Mohammad died in June 1863, a few weeks after his successful campaign to Herat. Following his death, a civil war ensued among his sons, prominently Mohammad Afzal Khan, Mohammad Azam Khan, and Sher Ali Khan. Sher Ali won the resulting Afghan Civil War (1863–1869) and ruled Afghanistan until his death in 1879. In his final years, the British returned to Afghanistan in the Second Anglo-Afghan War to fight perceived Russian influence in the region. Sher Ali retreated to northern Afghanistan, intending to create a resistance there similar to his predecessors, Dost Mohammad Khan, and Wazir Akbar Khan. His untimely death however, saw Yaqub Khan declared the new Amir, leading to Britain gaining control of Afghanistan's foreign relations as part of the Treaty of Gandamak of 1879, making it an official British Protected State. An uprising however, re-started the conflict, and Yaqub Khan was deposed. During this tumultuous period, Abdur Rahman Khan began his rise to power, becoming an eligible candidate to become Amir after he seized much of Northern Afghanistan. Abdur Rahman marched on Kabul, and was declared Amir, being recognized by the British as well. Another uprising by Ayub Khan threatened the British, where rebels confronted and defeated British forces in the Battle of Maiwand. Following up on his victory, Ayub Khan unsuccessfully besieged Kandahar, and his decisive defeat saw the end of the Second Anglo-Afghan War, with Abdur Rahman secured firmly as Amir. In 1893, Abdur Rahman signed an agreement in which the ethnic Pashtun and Baloch territories were divided by the Durand Line, which forms the modern-day border between Pakistan and Afghanistan. Shia -dominated Hazarajat and pagan Kafiristan remained politically independent until being conquered by Abdur Rahman Khan in 1891–1896. He was known as the "Iron Amir" for his features and his ruthless methods against tribes. He died in 1901, succeeded by his son, Habibullah Khan.

How can a small power like Afghanistan, which is like a goat between these lions or a grain of wheat between two strong millstones of the grinding mill, stand in the midway of the stones without being ground to dust?

During the First World War, when Afghanistan was neutral, Habibullah Khan was met by officials of the central powers in the Niedermayer–Hentig Expedition. They called on Afghanistan to declare full independence from the United Kingdom, join them and attack British India, as part of the Hindu–German Conspiracy. The effort to bring Afghanistan into the Central Powers failed, but it sparked discontent among the population about maintaining neutrality with the British. Habibullah was assassinated in February 1919, and Amanullah Khan eventually assumed power. A staunch supporter of the 1915–1916 expeditions, Amanullah Khan invaded British India, beginning the Third Anglo-Afghan War, and entering British India via the Khyber Pass.

After the end of the Third Anglo-Afghan War and the signing of the Treaty of Rawalpindi on 19 August 1919, Emir Amanullah Khan declared the Emirate of Afghanistan a sovereign and fully independent state. He moved to end his country's traditional isolation by establishing diplomatic relations with the international community, particularly with the Soviet Union and the Weimar Republic. He proclaimed himself King of Afghanistan on 9 June 1926, forming the Kingdom of Afghanistan. He introduced several reforms intended to modernize his nation. A key force behind these reforms was Mahmud Tarzi, an ardent supporter of the education of women. He fought for Article 68 of Afghanistan's 1923 constitution, which made elementary education compulsory. Slavery was abolished in 1923. King Amanullah's wife, Queen Soraya, was an important figure during this period in the fight for woman's education and against their oppression.

Some of the reforms, such as the abolition of the traditional burqa for women and the opening of co-educational schools, alienated many tribal and religious leaders, leading to the Afghan Civil War (1928–1929). King Amanullah abdicated in January 1929, and soon after Kabul fell to Saqqawist forces led by Habibullah Kalakani. Mohammed Nadir Shah, Amanullah's cousin, defeated and killed Kalakani in October 1929, and was declared King Nadir Shah. He abandoned the reforms of King Amanullah in favor of a more gradual approach to modernization, but was assassinated in 1933 by Abdul Khaliq.

Mohammed Zahir Shah succeeded to the throne and reigned as king from 1933 to 1973. During the tribal revolts of 1944–1947, King Zahir's reign was challenged by Zadran, Safi, Mangal, and Wazir tribesmen led by Mazrak Zadran, Salemai, and Mirzali Khan, among others – many of whom were Amanullah loyalists. Afghanistan joined the League of Nations in 1934. The 1930s saw the development of roads, infrastructure, the founding of a national bank, and increased education. Road links in the north played a large part in a growing cotton and textile industry. The country built close relationships with the Axis powers, with Nazi Germany having the largest share in Afghan development at the time.

Until 1946 King Zahir ruled with the assistance of his uncle, who held the post of prime minister and continued the policies of Nadir Shah. Another uncle, Shah Mahmud Khan, became prime minister in 1946 and experimented with allowing greater political freedom. He was replaced in 1953 by Mohammed Daoud Khan, a Pashtun nationalist who sought the creation of a Pashtunistan, leading to highly tense relations with Pakistan. Daoud Khan pressed for social modernization reforms and sought a closer relationship with the Soviet Union. Afterward, the 1964 constitution was formed, and the first non-royal prime minister was sworn in.

Zahir Shah, like his father Nadir Shah, had a policy of maintaining national independence while pursuing gradual modernization, creating nationalist feeling, and improving relations with the United Kingdom. Afghanistan was neither a participant in World War II nor aligned with either power bloc in the Cold War. However, it was a beneficiary of the latter rivalry as both the Soviet Union and the United States vied for influence by building Afghanistan's main highways, airports, and other vital infrastructure. On a per capita basis, Afghanistan received more Soviet development aid than any other country. In 1973, while the King was in Italy, Daoud Khan launched a bloodless coup and became the first president of Afghanistan, abolishing the monarchy.