Star Trek: The Next Generation/Recap/S6 E11 Ship in A Bottle

Data and Geordie are enjoying a Sherlock Holmes holodeck mystery, when Data's suave reveal is stopped in its tracks, as it turns out the suspect is apparently right-handed, when he's supposed to be left-handed. They stop the program and call Lieutenant Barclay in to check things out, who does a few diagnostics, then finds a locked program on the holodeck memory. When he asks it to run, who should appear but Professor Moriarty. It's been about four years since Data first granted him accidental sentience and he was subsequently left in the memory banks with a promise to find a way to free him, and he's not thrilled that it's taken so long, so he demands to meet Picard, who'd made the promise to help him.

In the mean time, the ship is monitoring the impending birth of a new star. Picard is called down to the holodeck, and with Data and Barclay, he enters to talk to Moriarty. Not only is he upset, but now he says that he can't trust Picard to work in his interest, and demands some sort of assistance to free himself from the Holodeck. Picard insists that it's impossible to get him out, and to demonstrate his point, he tosses a book through the open door, letting the simulated matter flash away into nothingness as it crosses the barrier. Moriarty claims that he is different, being alive, and that if he is just a computer simulation, it won't be a huge loss if he vanishes. So he steps beyond the door.

And he's just fine. He's out of the Holodeck, and doing just fine. The crew is understandably quite confused, and want to work to study how this was possible while Moriarty takes in the glory of the twenty-fourth century for the first time. Moriarty soon realizes that he's all alone in the future, though, and wants to to bring the Countess, a character written to be his love who he's truly fallen in love with on his own, into the real world with him. When he feels like the crew of the Enterprise isn't doing enough, he takes over the commands, demanding that they immediately devote their efforts to helping his love, or he'll let the ship drift into the star.

Barclay meets the charming Countess as they try their experiments with creating real matter from holodeck matter. The process is a failure, but Data hopes to learn something from the results, but when he looks at the results, nothing is there, as if they had never tried their experiment. Down in Engineering, LaForge tries to help Picard get back into control of the computer, but even after putting in his command override, it still won't respond. Data notices that Geordie is switching handedness, and explains to Picard that he now understands what has happened--when he, Picard and Barclay entered the holodeck for the first time, they never left afterward; the simulation was extended to include the ship, to think that they had returned. The lack of any results from their beaming experiment was because the real ship had no information on what would happen. And since they're still in the holodeck, now Moriarty has Picard's command codes. And he's locked them out of ending the program.

In real life, Moriarty contacts the bridge, having taken control of the ship, and demands that the crew start working on bringing them out of the holodeck for real--when Geordie and Riker sound like they don't think it'll work, he starts raising the temperature of the warp core until they agree to work on bringing him out into the actual real world. Picard goes to visit the Countess in the holodeck (within the holodeck that the simulated Enterprise is in) and asks her to talk to Moriarty about releasing the ship, appealing to her good nature, and offhandedly mentioning that they had found a way to Techno Babble their way to transporting holodeck matter. The Countess tells Moriarty about said technobabble, and Moriarty relays it to the real Enterprise's Riker.

Reluctantly, Riker beams Moriarty and the Countess aboard the real Enterprise, where they arrive safely. Moriarty demands a shuttlecraft for he and the Countess to leave to explore the stars in, and it is given to them. As they fly out into space, Moriarty gives his command override code to the Enterprise. Then Picard steps out, tells the computer to end the program, and Riker disappears, along with the shuttle bay, returning to the grid of the holodeck.

Because, as is revealed, Picard had programmed a holodeck inside of the simulated Enterprise--so when Moriarty thought he was transporting out of the simulated Enterprise to escape to reality, he was in fact calling out to another holodeck program, nested inside his own. With the command codes from him, Picard and the others are able to free themselves, and emerge from the real holodeck. Barclay pops a clear glowing cube out of the holodeck's computer as they leave.

The Enterprise moves back to safety, and Moriarty and the Countess are held in the cube, which is simulating the galaxy for them. Barclay puts the cube into a power source which will keep them running to live out their lives exploring their simulated microcosm. Picard makes an offhand mention about how their own universe could be just an elaborate simulation for some unknown purpose, and once everyone else has left, Barclay nervously says, "Computer, end simulation." Nothing happens, and he looks relieved as he leaves to put Moriarty and the Countess somewhere safe.

As the credits start to play, we get a shot of the star-to-be they've been watching finally forming into a star in a big burst of red light.