The Supreme Court on Friday found that prosecutors have to prove that an individual alleged to have illegally possessed a firearm must know that they are part of a group banned from having the gun in the first place.

In a 7-2 ruling, the justices sided with United Arab Emirates citizen Hamid Rehaif, who shot firearms at a gun range after he was dismissed from college over bad grades and told that his immigration status under his student visa would be terminated.

Federal law bans undocumented immigrants from possessing guns, and prosecutors charged Rehaif with that crime. The judge in the case told the jury that prosecutors did not need to prove that Rehaif knew he was in the country illegally, which he disputed.

Justice Stephen Breyer wrote in the majority opinion that prosecutors do need to prove that Rehaif knew of his immigration status, and that he would therefore be banned from possessing a gun.

"To convict a defendant, the government therefore must show that the defendant knew he possessed a firearm and also that he knew he had the relevant status when he possessed it," Breyer wrote.

Justices Ruth Bader Ginsburg, Sonia Sotomayor, Elena Kagan, Neil Gorsuch, Brett Kavanaugh and Chief Justice John Roberts joined the majority opinion.

Right-leaning Justices Samuel Alito and Clarence Thomas dissented.

In the majority opinion, Breyer pushed back against the government's argument that lawmakers don't typically require prosecuted individuals to know their own status. He said the statutes invoked by the government in arguments "differ significantly from the provisions at issue here."

"As we have said, we normally presume that Congress did not intend to impose criminal liability on persons who, due to lack of knowledge, did not have a wrongful mental state. And we doubt that the obligation to prove a defendant's knowledge of his status would be as burdensome as the government suggests," the opinion reads.

Alito was highly critical of the ruling in the dissenting opinion, noting that the federal gun statute at hand applies to individuals like convicted felons, stalkers and those who commit acts of domestic violence.

"Today's decision will make it significantly harder to convict persons falling into some of these categories, and the decision will create a mountain of problems with respect to the thousands of prisoners currently serving terms" for convictions under that law, he argued.

Alito also took issue with the argument that Rehaif may not have known he was in the country illegally, and criticized the court for agreeing to hear the case in the first place.

And he warned that the ruling could have ramifications for future cases on immigration.

"Serious problems will also result from requiring proof that an alien actually knew-not should have known or even strongly suspected but actually knew-that his continued presence in the country was illegal," Alito wrote.