WATCH: The U.S. president and commander in chief was making his first visit to Afghanistan since his inauguration in January 2009.
U.S. President Barack Obama has thanked American troops for their sacrifices in Afghanistan and told them he is confident they will succeed in their mission.
Obama was speaking to some 2,500 U.S. forces at Bagram Air Field after arriving in Afghanistan for his first visit as U.S. commander-in-chief.
The visit was shrouded in secrecy until Obama arrived in Afghanistan after dark following a through-the-night flight from the United States. He left the country six hours later.
To cheers from the troops, Obama hailed what he called their courage and heroism in a war now into its ninth year.
He said they were there to help Afghans "forge a hard-won peace," and that their work was vital to keeping Americans safe.
"We've seen violent extremists pushed out of their sanctuaries. We struck major blows against Al-Qaeda leadership as well as against Taliban," Obama said.
Obama warned that Al-Qaeda remained a threat, and said if the Taliban retook control of Afghanistan the United States would be less secure.
But he told the troops he was confident they "are going to get the job done."
"There are going to be some difficult days ahead. There are going to be setbacks. We face a determined enemy. We also know this -- the United States of America does not quit when it starts on something," Obama said.Karzai Meeting
On his arrival earlier in Kabul, the U.S. president went straight into a meeting with Afghan President Hamid Karzai at the presidential palace, at which he was expected to press Karzai to clamp down on the country's drug trade.
Afterward, Obama told reporters there had been progress in the military campaign against extremism in the region, but added that more was needed in the fight against corruption and in improving governance and rule of law.
Afghan President Hamid Karzai meets with Obama in Kabul.
Karzai said he hoped the partnership would help create "a stable, strong, peaceful Afghanistan that can sustain itself."
The White House also said Obama had invited Karzai to visit Washington on May 12 -- the first such invitation since Karzai began a new term following last year's fraud-marred elections.
Earlier, General James Jones, the U.S. national security advisor, told reporters aboard Air Force One that Obama planned to "engage President Karzai" in order to "make him understand that...in this second term that there are certain things he has to do as the president of his country to battle the things that have not been paid attention to almost since day one."
The White House said before the trip Obama wanted to get an "on the ground update" about the war from General Stanley McChrystal, the U.S. and NATO commander, as well as Karl Eikenberry, the U.S. ambassador.
The visit comes at the end of a momentous week for Obama, following the passage of his top domestic priority, health care reform, and the clinching of a key nuclear arms reduction treaty with Russia.
Obama visited Afghanistan during the 2008 U.S. presidential election campaign but has not been back since his victory over Republican Senator John McCain in November of that year.
White House officials say weather and logistical reasons had thwarted previous attempts at a presidential visit since Obama took office in January 2009.
Last year, Obama ordered the deployment of an extra 30,000 U.S. troops to Afghanistan and set a mid-2011 target to begin withdrawal.
Around one-third of the extra troops have now arrived, most of them sent to the south for a major offensive against the Taliban.
The trip allows Obama to see the early results of his troop increase, show support for military personnel, and counter critics who say his focus on passing healthcare legislation has diverted attention from foreign policy.