Gordon Brown was warned about taking controversial spin doctor Damian McBride with him from the Treasury when he became prime minister, a former Cabinet secretary said.

Lord O'Donnell, who was Cabinet secretary from 2005 until 2011, said the incoming prime minister chose to ignore his advice in 2007, which he now believed had been "damaging" to Mr Brown.

Mr McBride, who has recently published his memoir Power Trip, had been a special adviser to Mr Brown when he was both chancellor of the exchequer and prime minister. He quit in 2009 over his involvement in the plot to smear political opponents.

Prior to becoming Mr Brown's special adviser in 2005, he worked for him as a Treasury civil servant, when Lord O'Donnell was the department's permanent secretary

Lord O'Donnell, who was speaking at an event at the Cheltenham Literature Festival, said Mr McBride had "gone off the territory" - even as a civil servant.

"I had to, as it is clear in Damian's book, so I can talk about it now, Damian basically did not act as a civil servant," Lord O'Donnell said.

"He was briefing against other ministers, as he makes clear in his book, and that's inappropriate as a civil servant.

"I had to say to Gordon Brown 'no sorry, he cannot carry on doing what he's doing and he'll have to resign'.

"So he resigns as a civil servant and gets re-appointed as a special adviser. Special advisers are appointed by ministers and I can't do anything about that.

"I advised Gordon Brown when he was coming across to No 10 not to take Damian with him and he chose to. Again I think that was damaging for Gordon Brown."

Lord O'Donnell, who sits in the House of Lords as a crossbench peer, said that he "really liked" the idea of a "good" special adviser.

"They make the system work. Civil servants are by their nature there to be objective and not to get involved in any party issues," he said.

"Good special advisers will do those things that civil servants can't. I do think it is important though that special advisers do make a point of thinking 'my job is to help the government'. That's the good side.

"The bad side is when they think their job is to boost their minister at the cost of other ministers. That's when it goes wrong.

"You look at the special advisers that have breached that divide and briefing against minister 'x' because they are in dispute with minister 'y'. That damages the government and that's a very bad thing."