In 1890 he moved to Johannesburg and in 1896 he founded the Krugersdorpse Vrywilligerskorps experiencing combat with the Jameson Raiders. On the Uitlander issue, Viljoen was an ally of Paul Kruger. He is famously attributed to saying in the Volksraad that it was time to put trust in "God and the Mauser".
With the outbreak of the Second Anglo-Boer War he first did service in the Colony of Natal. He led the Johannesburg Commando, the great nemesis of the Uitlanders and he fought at the Battle of Elandslaagte on 21 October 1899. When the front disintegrated, he joined up with other Boers to stop the British march on Pretoria.
When British General Ian Standish Monteith Hamilton crossed the Vaal River on 26 May 1900, Viljoen and his Johannesburg Commando confronted him with De la Rey and the Lichtenburg Commando.
With the end of the conventional phase of the war, he formed a powerful guerrilla commando consisting of men from Johannesburg, Krugersdorp, Boksburg and the North and East Transvaal. His success resulted in him attaining the high rank of Assistant Commandant-General (November 1900). Viljoen's exploits included surprising a garrison at Helvetia in the Eastern Transvaal on 29 December 1900 and temporarily capturing 235 men and a 4.7-inch gun.
He was eventually captured at Lydenburg on 25 January 1902 and remained prisoner-of-war until May 1902 at the Broadbottom Camp, on St. Helena. It was there that he wrote his autobiography, "My Reminiscences of the Anglo-Boer War". The book is a realistic description of the war from a Boer perspective. Unlike other Boer generals he was not a property owner and thus hesitated to return to South Africa. He returned to South Africa as a pauper but refused to take British citizenship, thus greatly reducing his chances of resuming a public career. He was also disillusioned by rumours claiming that he had collaborated with the British, and he felt that his contribution to the struggle was not recognised.
Ben Viljoen was one of the South African refugee officers who formed a farm colony in Mexico with the assistance of Theodore Roosevelt. Help with selecting and negotiating for the property were provided by two men hired by Roosevelt family friend Marshall Latham Bond and the husband of a Roosevelt relative Edward Reeve Merritt. It was located at Hacienda Humboldt in the municipality of Julimes, Chihuahua.
He left for the United States in 1904 along with General Piet Cronjé (of Battle of Paardeberg fame) to take part in the Louisiana Purchase Exposition (St. Louis, Missouri) and the so-called "Boer War Circus" – portrayals of scenes from the Boer War. After leaving the fair, he settled down in the US. He separated from his wife, Lenie (née Els), who did not really want to leave South Africa and married an American woman, Myrtle Dickerson. Attempts to establish a Boer settlement in the north of Mexico ended in failure.
He returned from Mexico to the US and was instrumental in organising Boer colonies in Doña Ana County at Berino, Chamberino and La Mesa in New Mexico Territory. He became involved in civic affairs throughout the Mesilla Valley, and in 1909 he was granted American citizenship. Viljoen became familiar with both President Theodore Roosevelt and New Mexico Governor George Curry. Viljoen was commissioned as a Major in the territorial National Guard's First Regiment of Infantry. In 1911, he travelled to Washington, D.C., with Curry and Albert Bacon Fall as part of a delegation promoting statehood for New Mexico. In April and May 1911, he fought with the Mexican Revolutionary Francisco Madero at the Battle of Ciudad Juarez.
Viljoen was also influential in agriculture. He introduced new crops and farming practices to the Mesilla Valley. Viljoen was interested in the creation of the Elephant Butte Dam and developed irrigation systems for the valley.
For a short while, he was US Consul in Germany, and also acted as military advisor to Francisco Madero until Madero's assassination in 1913. He died in 1917, at his farm in La Mesa and is buried at the Masonic Cemetery in La Mesa, New Mexico.
For an insight of Ben Viljoen during the Anglo-Boer conflicts (as perceived by the English-speaking press), see 24 December 1899 New York Times article, "With the Boers at War."