The court made the decision after a case was brought by protester Samira Ibrahim.
She accused the Egyptian army of forcing her to undergo a virginity test after she was arrested during a protest in Tahrir Square in March.
Human rights organisations say the Egyptian military has used the practice widely as a punishment.
"The court orders that the execution of the procedure of virginity tests on girls inside military prisons be stopped," judge Aly Fekry, head of Cairo administrative court said, according to Reuters.
The ruling was greeted by cheers from hundreds of activists inside the courtroom.
Activists had demanded that the authorities prosecute anyone responsible for subjecting protesters to such tests.
Earlier this year, an Egyptian general was quoted as acknowledging that the military had conducted such tests, saying that they were used so women would not later claim they had been raped by authorities.
Human rights groups say such tests are a degrading form of abuse and the general's justification a legal absurdity.
A Cairo court has ordered forced virginity tests on female detainees in military prisons to be stopped.