Along his journey Monday morning toward a crowded subway tunnel below Midtown Manhattan, a would-be suicide bomber posted a statement to his Facebook page deriding President Trump, the federal authorities said on Tuesday. Then, among the throngs of commuters, he detonated a homemade pipe bomb affixed to his torso with the aim of inflicting as much death as possible.

The allegations were contained in a criminal complaint filed in federal court in Manhattan on Tuesday against the bombing suspect, Akayed Ullah, 27, an immigrant from Bangladesh who had lived for several years in Brooklyn. Because his crude bomb malfunctioned, he survived his own attack and has been charged with five federal counts that include the use of weapons of mass destruction, provision of material support to the Islamic State and bombing a place of public use.

The complaint said that Mr. Ullah, who has been held at Bellevue Hospital Center, admitted to investigators that he had built the pipe bomb and carried out the attack.

“I did it for the Islamic State,” he said, according to the complaint.

It said he also told his interrogators that one of his goals in carrying out the attack “was to terrorize as many people as possible.”