McCartney said the song was inspired by Apple scruffs (dedicated fans who hung around outside the Abbey Road studio and the homes of the Beatles), who broke into McCartney's St John's Wood home. Jessica Samuels, one of the groupies, says: "We were bored, he was out and so we decided to pay him a visit. We found a ladder in his garden and stuck it up at the bathroom window which he'd left slightly open. I was the one who climbed up and got in." She then opened the front door to let the others in, and they raided his wardrobe for a pair of trousers, which they took turns wearing. The scruffs also stole a number of photographs in addition to clothes. Another Apple scruff, Margo Bird, remembers being good friends with McCartney—she would often take his dog for walks—and later got a job at Apple Corps. She says that she was asked to retrieve a photograph of his father Jim, which she did.
In the 2006 documentary The Classic Artists Series: The Moody Blues, Mike Pinder, the former keyboard player of the Moody Blues, states that the inspiration for the song actually rests with an incident that happened to them—a groupie climbing into an open bathroom window in the band's home and spending the night with band member Ray Thomas. The next day, Pinder and Thomas recounted the story to McCartney, armed with his guitar, who strummed and sang "She came in through the bathroom window ..."
The rhythm track was recorded by the Beatles for this and "Polythene Pam" as one piece on 25 July 1969. After take 39, they added lead vocals, and re-recorded the drums and bass parts. On 28 July they added more vocals, guitar, percussion and piano. The song was completed two days later with additional guitar and percussion. "She Came In Through the Bathroom Window" segues abruptly after "Polythene Pam", the song on the preceding track, without pause. At the very beginning of the song, in anticipation of the change of tempo, John Lennon says "well listen to that Mal (referring to Mal Evans)... heh-heh" and then shouts "Oh, look out!"
A slower, ethereal version of this song, recorded during the Let It Be sessions, appears on Anthology 3.