WASHINGTON — Kirstjen Nielsen, the homeland security secretary, told colleagues she was close to resigning after President Trump berated her on Wednesday in front of the entire cabinet for what he said was her failure to adequately secure the nation’s borders, according to several current and former officials familiar with the episode.

Ms. Nielsen, who is a protégée of John F. Kelly, the White House chief of staff, has drafted a resignation letter but has not submitted it, according to two of the people. As the head of the Department of Homeland Security, Ms. Nielsen is in charge of the 20,000 employees who work for Immigration and Customs Enforcement.

Mr. Trump’s anger toward Ms. Nielsen, who was sitting several seats to his left at the meeting, was part of a lengthy tirade in which the president railed at his cabinet about what he said was its lack of progress toward sealing the country’s borders against illegal immigrants, according to one person who was present at the meeting.

Asked about the heated exchange at the meeting, Sarah Huckabee Sanders, the White House press secretary, said Thursday that “the president is committed to fixing our broken immigration system and our porous borders.”

In a statement, Ms. Nielsen said she intended to “continue to direct the department to do all we can to implement the president’s security-focused agenda.” She said Mr. Trump was “rightly frustrated that existing loopholes and the lack of congressional action have prevented this administration from fully securing the border.”

Tyler Q. Houlton, a spokesman for the Department of Homeland Security, disputed that Ms. Nielsen had drafted a resignation letter and was close to resigning, calling those assertions “false.”