Whenever the Messenger of Allah ﷺ entered the lavatory, he would say, "O Allah, I seek refuge in You from male and female devils."
أخبرنا إسحق بن إبراهيم قال أنبأنا إسمعيل عن عبد العزيز بن صهيب
كان رسول الله ﷺ إذا دخل الخلاء قال اللهم إني أعوذ بك من الخبث والخبائث
Agreed upon (Bukhari and Muslim). Bukhari, Book of Wudu, Chapter on what to say when entering the toilet, hadith (142), from the hadith of Abdul-Aziz, through him. And Muslim, Book of Menstruation, Chapter on what to say when intending to enter the toilet, hadith (375), from the hadith of Ismail bin Ulaya, through him. It is also in Al-Kubra, hadith (19).