BRENTWOOD, N.H. (CBS/AP) – A man accused of shooting two Manchester police officers has pleaded not guilty by reason of insanity.

A judge sentenced Ian MacPherson to five years in a psychiatric unit during a plea and sentencing hearing on Thursday.

READ MORE: Bruins Rule Out Matt Grzelcyk For Monday Night's Game Vs. Ducks

A judge sentenced Ian MacPherson to five years in a psychiatric unit. (WBZ-TV)

“This is the best resolution,” said New Hampshire Assistant Attorney General Jeff Strelzin. “The hope is the defendant gets the help he needs and doesn’t hurt anyone in the future.”

Police said MacPherson shot officers Ryan Hardy and Matthew O’Connor minutes apart as they pursued him in May 2016. Both survived.

Manchester, New Hampshire Police Officer Ryan Hardy was released from Brigham and Women’s Hospital on May 14, 2016. (WBZ-TV)

“It’s an honor to lead a group of men and women who are so courageous, heroic and selfless and doing it for the betterment of their community,” Manchester Police Chief Nick Willard said.

Hardy stopped MacPherson because he matched the description of an armed robbery suspect. MacPherson began shooting at Hardy as soon as the officer announced himself, investigators said.

Hardy was shot in the face and torso.

Officer Matthew O’Connor was shot in the leg and was on crutches May 16, 2016. (WBZ-TV)

READ MORE: Harmony Montgomery's Stepmother 'Knows What Law Enforcement Is Closing In On Learning,' Prosecutors Say

About 20 minutes later, MacPherson encountered O’Connor and shot him in the leg, police said.

MacPherson was captured after a manhunt that shut down the west side of the city. He has been held without bail.

Back in 2016, MacPherson’s father said his son had been suffering from mental illness issues for a “long, long time.”

Ian MacPherson. (Photo credit: Manchester, NH Police)

Chief Willard expects MacPherson will be committed for the rest of his life.

“I feel confident, given the diagnosis I’m aware of, that he’ll most likely spend the rest of his life in a psychiatric unit and be away from society to not harm another person,” Chief Willard said.

Both officers who were shot are back on the job.

MacPherson appeared Thursday in Rockingham County Superior Court in Brentwood.

MORE NEWS: Man Shot At South Shore Plaza In Braintree Dies

(© Copyright 2018 CBS Broadcasting Inc. All Rights Reserved. The Associated Press contributed to this report.)