WASHINGTON — The Obama administration on Friday directed Volkswagen to recall nearly a half-million cars, saying the automaker illegally installed software in its diesel-power cars to evade standards for reducing smog.

The Environmental Protection Agency accused the German automaker of using software to detect when the car is undergoing its periodic state emissions testing. Only during such tests are the cars’ full emissions control systems turned on. During normal driving situations, the controls are turned off, allowing the cars to spew as much as 40 times as much pollution as allowed under the Clean Air Act, the E.P.A. said.

“We expected better from Volkswagen,” said Cynthia Giles, the E.P.A.’s assistant administrator for the Office of Enforcement and Compliance. She called the automaker’s actions “a threat to public health.”

Agency officials issued the car company a notice of violation and said it had admitted to the use of a so-called defeat device. The recall involves 4-cylinder Volkswagen and Audi vehicles from model years 2009-15.