People “are increasingly exposed to the crazy pricing of health care,” said Chas Roades, one of the founders of Gist Healthcare, a consulting firm in Washington. “We are overdue for a public airing of how all of this works.”

While the hospitals’ legal challenge may succeed, they are vulnerable to the increasing public outcry over high prices, Mr. Roades said. “It’s not a good look for the industry to push back on transparency on prices.”

Mr. Trump seemed to relish taking on powerful interest groups. “I don’t know if the hospitals are going to like me too much anymore with this, but that’s O.K., right?” he asked. About health insurers, he quipped, “they’ll be thrilled.”

The new rule requires hospitals to make a range of prices easily available, such as prices negotiated within an insurer's network, what hospitals are paid if their care is out of a patient’s insurance network, and what the hospital would accept for the treatment if paid in cash.

Administration officials, employers and others have criticized hospitals and insurers for keeping the deals they strike a secret, making it challenging for patients to seek less expensive care. They argue that by making it easier for people to find the actual prices that insurers pay — and not just the standard list prices for various services, which the Trump administration started requiring hospitals to post earlier this year — hospitals will be under more pressure to compete on prices.

The rule has the potential to roil the health care industry, which critics argue use the secrecy of their negotiations to keep prices high. A recent study showed that private insurers pay some hospitals two to three times more than the federal Medicare program pays for the same care. Even employers say they have little visibility into the prices being paid by the insurers on behalf of their workers.

[Like the Science Times page on Facebook. | Sign up for the Science Times newsletter.]

Proponents of the rule say it will create a working health care market in which hospitals compete on price and quality. People will be able to look for hospitals that give them the best value, said Cynthia Fisher, a health care entrepreneur and the founder of the group Patient Rights Advocate. “It will be aggregated and assimilated in a way that people can have ready access to it,” she said.