Internal conflicts so weakened the amirs' authority that in 382/992 Nuh b. Mansur was unable to halt an invasion of Transoxiana by the Qarakhanid chief Bughra Khan Harun, who for a time actually occupied the capital Bukhara. An alliance against the crown of two great men in the state, Abu 'All Simjuri and the Turkish general Fa'iq Khassa, drove Amir Nuh to call in Sebuk-Tegin in the hope of redressing the balance (384/994). Sebiik-Tegin and Mahmiid now appeared in Khurasan and routed the rebels; both consequently received a grant of honorific titles from the grateful amir, and Mahmiid was invested with command of the army of Khurasan. By 385/995 rebel opposition had been temporarily crushed, and Khurasan was in Mahmud's hands; once Mahmiid was secure on the throne of Ghazna three years later, Khurasan was to be an integral part of the Ghaznavid empire for the next forty years. However, the shrinking Samanid dominions continued to be disordered: the Qarakhanids took over the whole of the Syr Darya basin, and the authority of the amirs was confined to a small part of Transoxiana.
In the midst of this, Sebuk-Tegin died (387/997), and Malimud was obliged to leave Khurasan and allow the Turkish general Bektuzun to occupy Nishapiir. Sebuk-Tegin had appointed as his successor in Ghazna a younger son, Isma'Il (possibly because Isma'il's mother was a daughter of Alp-Tegin), and the claims of the more experienced and capable Mahmud were ignored. Mahmud proposed a division of power within the Ghaznavid territories, but Isma'Il refused this; recourse to arms followed, and after a few months' reign in Ghazna, Isma'll was deposed (388/998). The Samanid Amir Abu'l-Harith Mansur b. Nuh now confirmed Mahmud in possession of Ghazna, Bust and the eastern Khurasanian towns of Balkh, Tirmidh and Herat, but Mahmud was left to recover western Khurasan from Bektuzun. The deposition of the amir by Bektuzun and Fa'iq enabled Mahmud to pose as his avenger, and after further negotiations and renewed fighting, Mahmud was in 3 89/999 a t l a st victorious over all his enemies. Khurasan was now firmly within his possession, and with the advance of the Qarakhanid Ilig Nasr to Bukhara in the same year, the Samanid dynasty virtually ended.
Mahmud established friendly relations with the Ilig, and both sides agreed that the former Samanid dominions should be partitioned, with the Oxus as boundary between these two Turkish powers. This cordiality proved to be only transient; very soon, the Qarakhanids were trying to extend their authority into Khurasan, whilst Mahmud later tried to secure a foothold north of the Oxus. Significant for the future orientation of Ghaznavid policy was Mahmud's eagerness to secure legitimization of his power from the 'Abbasid caliph al-Qadir, who at this point sent him the honorific by which he became best known, that of Yamin al-Daula "Right hand of the state". The Ghaznavids were always careful to buttress their authority by caliphal approval and by an ostentatious espousal of the cause of Sunni orthodoxy.
Mahmud's thirty-two years' reign (388-421/998-1030) was one of ceaseless campaigning and warfare over a vast stretch of southern Asia; at his death, the empire stretched from the borders of Azarbaijan and Kurdistan in the west to the upper Ganges valley of India in the east, and from Khwarazm in Central Asia to the Indian Ocean shores. Not since the early days of the 'Abbasid caliphate had such a vast assemblage of territories been ruled by one man. This was an entirely personal creation and consequently ephemeral, for Mahmud's son Mas'ud was inferior to his father in skill and judgement and was unable to hold the empire together. Yet the might of Mahmud's empire at its zenith immensely impressed succeeding generations of Muslims, and especially excited the admiration of those who held fast to Sunni orthodoxy and revered the 'Abbasid caliphs as imams of the community of the faithful. It was fortunate for Mahmud that his campaigns on both flanks of the empire could so often be represented in a favourable religious light. In the east, Mahmud achieved his reputation as the great ghdttf sultan and hammer of the infidel Hindus.
That his motives here were, as is explained below, as much influenced by material as spiritual considerations did not affect the approbation of contemporaries, who knew only that such houses of abomination as the great idol-temple of Somnath were being cleansed, just as Muhammad the Prophet had purified the Ka'ba of its 365 idols. In the west, Mahmud's main opponents were the Buyids and lesser Dailami powers like the Kakuyids of Isfahan and Hamadan and the Musafirids of Dailam, and since these were Shfi in faith, it was possible to publicize Mahmud's campaign of 420/1029 in western Persia as a crusade for the re-establishment of Sunni orthodoxy.