President Donald Trump has formally signed a presidential memo directing the Pentagon to ban transgender people from joining the US military, following through on a policy he announced on Twitter back in July.

The presidential memo, issued late Friday evening, directs the secretaries of defense and homeland security (which oversees the US Coast Guard) to put forward a plan to implement the new policy by February 21, 2018.

While the order is unequivocal when it comes to banning all new transgender recruits, it gives Secretary of Defense James Mattis, in consultation with the secretary of homeland security, the authority to determine if transgender people who are already serving in the armed forces can stay, based on “military readiness.”

But it does order the Pentagon to stop paying for any transgender-related medical treatments, including sex reassignment surgeries, for currently serving troops, “except to the extent necessary to protect the health of an individual who has already begun a course of treatment to reassign his or her sex.”

There are currently an estimated 2,150 to 10,790 active-duty and reserve transgender troops, according to a 2016 RAND study.

It’s worth noting there currently is no homeland security secretary. The last one, retired General John Kelly, was sworn in as Trump’s new chief of staff on July 31, replacing Reince Priebus. So it’s unclear if Mattis is expected to consult with the acting homeland security secretary, Elaine Duke, on coordinating the new policy or wait until a new homeland security secretary is confirmed by the Senate.

Strangely, the memo also seems to suggest that Trump may be open to having his mind changed. The memo states that “The Secretary of Defense, after consulting with the Secretary of Homeland Security, may advise me at any time, in writing, that a change to this policy is warranted.”

But even if Trump doesn’t change his mind, the administration is almost certain to face a tough legal and political fight over this order.

The president has been sued over the ban. He could win the case.

Shortly after Trump first announced the ban on Twitter, lawyers representing five active-duty transgender troops filed a lawsuit challenging the policy.

The names of the five troops have not been disclosed — they collectively go by “Jane Doe” — but they serve in the Air Force, Army, and Coast Guard. Some have been in the military for about 20 years and deployed to Iraq and Afghanistan.

The National Center for Lesbian Rights (NCLR) and GLBTQ Legal Advocates and Defenders are representing the plaintiffs. They argue that the ban doesn’t give transgender service members equal protection and due process.

“Every governmental policy must have at a minimum some legitimate basis. That is so plainly lacking here,” Shannon Minter, the NCLR’s lead attorney for this case (who is transgender himself), told me in an interview. “The policy that the president intends to impose would single out a disfavored group for discriminatory treatment.”

But Eugene Fidell, a military justice expert at Yale Law School, warns that the executive branch usually wins cases involving the government’s decisions to hire and fire people. “The tendency of federal courts is to defer to executive decision-making on military personnel issues,” he said. “The hardest thing courts have to do is to tell the government it must retain someone it doesn’t want to keep.”

Still, more transgender service members are likely to bring additional lawsuits, which means the administration will have to prepare for not one but many legal fights — any one of which could end up going all the way to the Supreme Court. The current makeup of the court leans conservative, however, so experts like Fidell believe it would probably keep the ban in place.

That means the likeliest way to block the ban is if Congress votes to do so.

Sen. Kirsten Gillibrand (D-NY), along with Sen. Cory Booker (D-NJ), plans to put forward legislation to block the ban once Congress is back in session. And a spokesperson for Sen. Tammy Duckworth, who was injured after her helicopter was hit by a rocket-propelled grenade in Iraq, told me the senator "intends to support" the legislation. The specifics of the legislation are still unknown.

But any effort to block the ban legislatively will fail without support from the Republicans who control both houses of Congress. And while many GOP lawmakers came out against the move after the initial Trump tweets, including Sens. John McCain (R-AZ) and Orrin Hatch (R-UT), it’s unclear whether they’d actually be willing to join Democrats in voting to block a policy of the president from their own party.

Which means that, at least for now, the policy will go forward.