The Scottish Government has admitted it mishandled complaints in relation to sexual harassment allegations made against Alex Salmond.

The former first minister took legal action to contest the complaints process during a hearing at the Court of Session in Edinburgh on Tuesday morning.

Two allegations, which he strongly denies, were made in January 2018, according to the Scottish Government.

Ronnie Clancy QC, speaking for Salmond, said the formal complaints against him were unlawful, unfair and "tainted by bias".

He added the government investigating officer's notes included evidence of them "assisting the complainers" and strays into realm of "giving them encouragement".

Mr Clancy said the government had "conceded the illegality" of its investigating officer's previous involvement in the Salmond case and said this made whole process "unfair".

The government's lawyer admitted it had made a critical mistake and breached its own guidelines.

Judge Lord Pentland said the decisions were "unlawful in respect that they were procedurally unfair and that they were tainted with apparent bias".

Salmond was also awarded all expenses.

Speaking outside court, Salmond thanked the people who raised more than £100,000 to a crowdfunding appeal to help pay for his legal challenge to the government's handling of the case.

He said the money will go to good causes in Scotland and elsewhere.

Salmond said: "The last time I was in that court was to be sworn in as first minister of Scotland.

"I never thought it possible that at any point I would be taking the Scottish Government to court.

"And therefore while I am glad about the victory which has been achieved today, I am sad that it was necessary to take this action."

He then called on Scotland's top civil servant to quit her post after accusing her of "wasting" hundreds of thousands of pounds of public money defending the administration's "unlawful" handling of sexual misconduct allegations against him.

Salmond said: "I'm obviously glad, delighted, by the result today.

"The government has made an abject surrender in terms of the case."

He claimed Leslie Evans, the permanent secretary to the Scottish Government, was responsible for the "institutional failure" in the handling of the complaint.

And he warned the case could cost the public purse as much as £500,000.

"Because the process has been agreed as unlawful, as unfair and tainted by apparent bias, then the Scottish Government have had to concede on the case and the expenses to the maximum extent," he said.

"That is going to raise a cost to the public purse of many, many hundreds of thousands of pounds."

Salmond added: "The process has already been admitted as unlawful, unfair and tainted by bias. You couldn't get more tainted.

"So when she has got some time for mature reflection, I hope that the Permanent Secretary considers her position, not events in the future.

"I cannot think on a day of abject humiliation for the Scottish Government that seems to me like a correct and proper response."

Salmond quit the SNP after bringing the legal challenge, but he said today it was now his intention to rejoin the party he had once led.

He added: "I'm not putting out the bunting today. Yes, I'm glad to have won, I'm really really sad to be forced to take this action against a government I led for almost eight years."

Mr Salmond said this was an "extraordinary and desperate" statement as options for this had been available during the case.

He indicated he is considering further legal action against the permanent secretary.

Permanent secretary Leslie Evans said an internal review would be carried out by the Scottish Government.

She said: "The single procedural flaw which led to this decision is deeply regrettable.

"In particular, I regret the distress it will cause to the two women who raised the complaints."

Ms Evans stated: "The Scottish Government has acted in good faith at all times and will continue to do so.

"It was right and proper that these complaints were investigated and I stand by the decision to carry out that investigation.

"It is also important to note that the procedural flaw in the investigation does not have implications, one way or the other, for the substance of the complaints or the credibility of the complainers.

"The Judicial Review was never about the substance of the complaints, but about the process that took place to investigate those complaints."

As a result she said it was open to the Scottish Government to re-investigate the complaints, adding that "subject to the views of the complainants, it would be our intention to consider this".

But Ms Evans said this would "only be once ongoing police inquiries have concluded".

The permanent secretary was defended by Nicola Sturgeon, who said it was not her view that Ms Evans should leave her post.

The First Minister told STV News: "Two complaints came forward about Alex Salmond. The permanent secretary was right to investigate them, not sweep them under the carpet.

"That was done under a procedure that the Scottish Government considers was and is robust, but in one respect the application of that procedure was found to be flawed.

"The permanent secretary, I know, deeply regrets that and is determined to learn lessons.

"She's instructed a review of the operational application of that procedure."

She added: "I think it's fair to say, in the last number of months, all organisations have grappled with how best to deal with allegations that are often historic in nature.

"So the Scottish Government will continue to do everything we can to ensure that we are leading the way in that and learning lessons when we get it wrong."

Pressed on if she thought Ms Evans should resign, Sturgeon replied: "No, that's not my view. She deeply regrets the situation. She recognises the Scottish Government as an institution has lessons to learn.

"I think it's important to remember, not in seeking to underplay the significance of today's developments but to put it in context, the procedure that was in place is in our view completely robust and the process, overall in our view, all other aspects of the process, were fair.

"But in one particular aspect there was the possibility of an impression being created that it wasn't completely impartial.

"There is no suggestion that in fact the process was impartial."

A separate police investigation into the allegations is ongoing.

A Police Scotland spokesperson said: "Our enquiries continue, we will not be commenting further."

Salmond resigned from the SNP during August in a video posted on YouTube.

In the clip, he said: "I read carefully Nicola Sturgeon's statement and watched her television interview. She made it clear that the SNP have never received a single complaint about my conduct in my many decades of membership.

"And the Scottish Government have confirmed they did not have any such complaint before this January - more than three years after I left office.

"So let me be clear again - I refute these two complaints of harassment, and I absolutely reject any suggestion of criminality.

"I believe that all such issues must be treated seriously, confidentially and through a fair process. In this case, confidentiality has been broken, greatly to my detriment, but also in a way that now puts at serious risk the anonymity of both complainants.

"It urgently needs to be established - who breached that duty of confidence? And why?"

Salmond said he planned to reapply for SNP membership when he "clears his name".