The Prophet ﷺ, whenever he entered the restroom, would say, "O Allah, I seek refuge in You from male and female devils."
كان النبي ﷺ إذا دخل الخلاء قال: اللهم إني أعوذ بك من الخبث والخبائث.
Bukhari related it in the Wudu chapter (142), from Adam, who said, Shuba narrated to us, from Abdul-Aziz bin Suhayb, who said, I heard Anas say... and he mentioned it.Bukhari said, Ibn Urwa followed him, 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 narrated to us, "When he wanted to enter," end of quote.Muslim also related it in the chapter on menstruation (375), from the hadith of Hammad, from Abdul-Aziz bin Suhayb, in similar wording.He also related it from the hadith of Hushaym, from Abdul-Aziz, with the wording, "When he entered the privy."