San Francisco filed a similar lawsuit.
Santa Clara County receives about US$1.7 billion in federal and federally dependent funds annually, about 35 per cent of its total revenues.
The county argued it was owed millions of dollars of federal funding every day and that its budgetary planning process had been thrown into disarray by the order.
The Justice Department said the counties had taken an overly broad interpretation of the President's order, which would affect only Justice Department and Department of Homeland Security funds, a fraction of the grant money received by the counties.
In his ruling, Orrick said the language of the order made it clear it sought to withhold funds beyond law enforcement.
"And if there was doubt about the scope of the Order, the President and Attorney-General have erased it with their public comments," Orrick wrote.
The judge cited comments from Trump calling the order "a weapon" to use against jurisdictions that disagree with his immigration policies.
- Reuters, AAP