The Prophet ﷺ, when he entered the lavatory, would say, "O Allah, I seek refuge in You from male and female devils."
كان النبي ﷺ و إذا دخل الخلاء قال: اللهم إني أعوذ بك من الخبث والخبائث.
Bukhari narrated it in the Book of Wudu (142), from Adam, who said, Shuba told us, from Abdul-Aziz bin Suhayb, who said, I heard Anas say... and he related it. Bukhari said, Ibn Urwa followed him in narrating it from Shuba. Ghundar said, from Shuba, "When he came to the toilet." Musa said, from Hammad, "When he entered the toilet." Saeed bin Zayd said, Abdul-Aziz told us, "When he wanted to enter" end of quote.Muslim also narrated it in the Book of Menstruation (375), from the hadith of Hammad, from Abdul-Aziz bin Suhayb, similarly. He also narrated it from the hadith of Hushaym, from Abdul-Aziz, with the wording, "When he entered the latrine."