Stardate 4729.4: The Enterprise arrives at a space station and is told that the person with their orders is beaming aboard. Kirk and Spock go to the transporter room to meet with Commodore Bob Wesley. Kirk is aware that most of his crew are being moved to a security holding center, and Wesley tells Kirk that Wesley will be commanding an attack force against the Enterprise Dr. Richard Daystrom is attaching his M-5 computer to the ship, a computer capable of correlating starship control. The M-5 will put the ship through navigational, exploration, and war game maneuvers. Only 20 crew will remain aboard the Enterprise, with Daystrom supervising the tests.
McCoy is less then thrilled about a machine being put in charge of the ship, arguing with Spock. Kirk isn't very happy either, and they meet with Daystrom in Engineering. He hooks up the M-5 to the engines and the power fluctuates briefly, but Daystrom compensates and explains that M-1 through 4 were not entirely successful. When Kirk insists there are things men must do, Daystrom wonders if Kirk might be worried about losing his position. Kirk insists its wrong and McCoy agrees, reassuring him he's not so petty as to be upset at the loss of prestige.
The M-5 takes the ship through basic movements then approaches a planet, then starts shutting down non-essential life support systems. Kirk gives a recommendation for the landing party, and the M-5 then gives its own recommendation. The computer picks two other members based on its knowledge of their background, and leaves Kirk and McCoy out of the landing party as non-essential personnel.
Upon getting word of the power shutdowns, Daystrom confirms there is no malfunction but M-5 shutting down unnecessary power, while drawing more power for itself. Daystrom insists the M-5 is a whole new approach. They pick up two ships on a parallel course and get word that it's an unscheduled drill. The two starships Excalibur and Lexington move to intercept and the M-5 attacks both ships with reduced phaser power. Wesley confirms the M-5 is considered the victor and sends his congratulations to "Captain Dunsel." Kirk leaves the bridge and Spock confirms that a "dunsel" is an Academy term for a useless part.
McCoy visits Kirk in his quarters and offers drinks, while the captain admits he felt useless. They get word of contact with a large slow-moving ship. Kirk returns to the bridge as the M-5 identifies it as the Woden, an old-style ore freighter. The M-5 moves to intercept and opens fire at full blast. When Kirk tries to override, nothing happens: the M-5 has taken control. An angry McCoy and Kirk confront Daystrom and tell him to shut down the M-5. They go to Engineering to shut it down but find that it's surrounded itself with a force field. Scotty sends a crewman to cut off the computer's power at its source, and the M-5 fires a disintegration beam, killing the man.
As M-5 draws more power, and takes control of the entire ship, Spock and Scotty try to reroute the navigational controls, while McCoy talks to Daystrom. Daystrom insists that his "child" shouldn't be shut down. He reveals to Kirk that Daystrom may be unstable and trying to live up to his past successes. Spock tells them that he's ready and they go to watch him disrupt the circuits. Daystrom tries to stop them but Kirk holds him back. However, it turns out that M-5 has been feeding an electrical current to convince them the circuit was active, and they have failed to regain control. Spock insists the computer isn't operating logically and Kirk demands answers. Daystrom explains that he impressed human engrams on computer circuits.
The four starships approach the Enterprise, unaware that M-5 will destroy them. It won't let Kirk hail the task force and Daystrom insists M-5 knows it's a drill. However, the computer brings phasers and deflectors to full power and moves to attack. It destroys the Excalibur and seriously damages the Lexington. Wesley tries to make contact and get them to stop the attack but Kirk can't respond. Daystrom doesn't know of any way to get through to the M-5, and Spock determines that Daystrom impressed his own engrams on the unit. They suggest he talk to his computer while they get word that Wesley is calling Starfleet and plans to destroy the Enterprise. Daystrom converses with the computer, insisting that murder is a sin and is clearly on the edge of a breakdown. The M-5 refuses to listen to him, insisting it must survive, and Daystrom goes berserk. Spock renders him unconscious as the three remaining starships close in.
Kirk talks directly to M-5 which insists it must survive, and cannot murder. Kirk points out that it has murdered the crews of the four starships, and asks how it will pay for its crimes. M-5 says it must die and shuts down entirely, lowering shields and phasers. Scotty disengages all of the connections while Kirk informs the crew that they will be dying for a good cause. However, Commodore Wesley notices the lack of power and calls off the attack.
Later, Daystrom will be taken to a rehabilitation center, with M-5 in similar conditions. Kirk gambled on Wesley's compassion and humanity. McCoy boasts about human virtues saving the day, while Spock can only express amusement at the idea of McCoy's engrams being converted into a computer.