Virginia Gov. Terry McAuliffe issued a sweeping order in April restoring rights to all ex-offenders who are no longer incarcerated or on probation or parole. (Jonathan Ernst/Reuters)

Gov. Terry McAuliffe’s decision to restore voting rights to more than 200,000 felons violates Virginia’s constitution, the state Supreme Court ruled Friday, dealing a major blow to the Democratic governor with implications for the November presidential race in the crucial swing state.

In a 4-to-3 decision, the court ruled that McAuliffe overstepped his clemency powers by issuing a sweeping order in April restoring rights to all ex-offenders who are no longer incarcerated or on probation or parole.

The court agreed with state Republicans who challenged McAuliffe’s order, arguing that the governor can only restore voting rights on a case-by-case basis and not en masse.

“Never before have any of the prior 71 Virginia Governors issued a clemency order of any kind — including pardons, reprieves, commutations, and restoration orders — to a class of unnamed felons without regard for the nature of the crimes or any other individual circumstances relevant to the request,” Chief Justice Donald W. Lemons wrote for the majority. “To be sure, no Governor of this Commonwealth, until now, has even suggested that such a power exists.”

But a defiant McAuliffe released a statement late Friday saying that he would pick up his executive pen and restore the rights of those felons on an individual basis, even if it means signing more than 200,000 orders.

“My faith remains strong in all of our citizens to choose their leaders, and I am prepared to back up that faith with my executive pen,” he said. “The struggle for civil rights has always been a long and difficult one, but the fight goes on.”

The court directed the state elections commissioner, Edgardo Cortés, to cancel the registrations by Aug. 25 of about 13,000 felons who had joined the voter rolls after McAuliffe signed his order. Cortés was also ordered to add their names to the list of prohibited voters.

McAuliffe said in his statement that he would “expeditiously” sign individual orders for those 13,000 felons and then keep on signing.

“Once again, the Virginia Supreme Court has placed Virginia as an outlier in the struggle for civil and human rights,” McAuliffe said. “It is a disgrace that the Republican leadership of Virginia would file a lawsuit to deny more than 200,000 of their own citizens the right to vote. And I cannot accept that this overtly political action could succeed in suppressing the voices of many thousands of men and women who had rejoiced with their families earlier this year when their rights were restored.”

The ruling comes three months after McAuliffe stood on the portico of the state capitol and pledged to erase the last vestiges of Jim Crow-era laws that disenfranchised African American voters. Nearly a quarter of the state’s black population cannot vote because of felony convictions.

Virginia is one of just a handful of states that ban all felons from voting and require individual exemptions for ex-offenders to vote, according to the Brennan Center for Justice. The ban is tinged with racial overtones: When it was adopted in 1902, a delegate testified to the need to “eliminate the darkey as a political factor,” according to Kristen Clarke, president of the Lawyers’ Committee for Civil Rights Under Law.

McAuliffe said his restoration order was his “proudest moment as governor,” and the state party has rallied around the policy as the premier achievement of his term.

But state Republicans saw it as a partisan move to swell the numbers of Democratic voters heading into the November election, when McAuliffe’s good friend, Hillary Clinton, will be battling to win the swing state and its 13 electoral votes in her presidential race against Republican Donald Trump.

Virginia House of Delegates Speaker William J. Howell (R-Stafford) and Senate Majority Leader Thomas K. Norment Jr. (R-James City), who brought the challenge with four voters, applauded the ruling.

“The Supreme Court of Virginia delivered a major victory for the Constitution, the rule of law and the Commonwealth of Virginia. Our nation was founded on the principles of limited government and separation of powers,” they said in a joint statement. “Those principles have once again withstood assault from the executive branch. This opinion is a sweeping rebuke of the governor’s unprecedented assertion of executive authority.”

Lemons noted in his opinion that Virginia’s last Democratic governor, Timothy M. Kaine, declined in 2010 to issue a blanket voting rights restoration order on advice from a senior adviser who said such a move would be an improper “rewrite” of the law and constitution. A spokeswoman for Kaine, now a senator who was announced as Clinton’s running mate Friday evening, did not return a request for comment.

John Whitbeck, chairman of the Virginia GOP, said in a statement that McAuliffe was trying to “stack the deck for Hillary Clinton” and accused him of a “naked power grab.”

Along with voting rights, the governor’s action restored the right to serve on a jury, run for public office and become a notary public.

Civil rights and progressive groups condemned the ruling.

“It’s disgraceful that some politicians would go to such lengths to block these citizens from participating in our democracy,” said Anna Scholl, executive director of Progress Virginia. “We would be happy to send Governor McAuliffe a box of pens to start signing individual orders.”

Voting access has become a heated topic across the country in a presidential election year that is expected to be especially close and hotly contested. Democrats have been pushing to loosen voting restrictions, saying they disproportionately affect poor and minority voters, while Republicans have been trying to tighten requirements in an effort they say is aimed at combating fraud. Earlier this week, a federal court struck down a voter identification law in Texas, finding it discriminated against minorities.

Tram Nguyen, co-executive director of New Virginia Majority, which dispatched staff to begin registering voters minutes after McAuliffe’s order, called Friday’s ruling “heartbreaking” for felons who had felt redeemed.

“They had a glimmer of hope, and now it’s being completely dashed and taken away,” she said.

But McAuliffe had also faced bipartisan criticism about the way his administration implemented the restoration order and for the decision to keep secret the list of felons who had their rights restored.

It was discovered that the list mistakenly included several violent criminals still in custody and 132 sex offenders under involuntary supervision. These errors drew ire from Democratic and Republican prosecutors who say they were not properly consulted ahead of the governor’s order and were tasked with finding errors.

McAuliffe’s order also made it simpler for felons to apply for the right to possess guns.

The Friday ruling also shows the consequences of a protracted and bitter battle in the past year over installing a state Supreme Court justice.

State lawmakers ousted a judge appointed by McAuliffe when the legislature was out of session and installed a preferred alternative. That judge, Stephen R. McCullough, joined three other conservative judges in overturning McAuliffe’s order.