Whenever the Messenger of Allah ﷺ entered the lavatory, he would say, according to Hammad's narration, "O Allah, I seek refuge in You," and according to Abd al-Warith's narration, "I seek refuge in Allah from male and female devils."
حدثنا مسدد بن مسرهد حدثنا حماد بن زيد وعبد الوارث عن عبد العزيز بن صهيب
كان رسول الله ﷺ إذا دخل الخلاء قال عن حماد قال اللهم إني أعوذ بك وقال عن عبد الوارث قال أعوذ بالله من الخبث والخبائث
Narrated by Muslim in the Book of Menstruation, chapter on what to say when intending to enter the toilet, hadith 375, from the narration of Hammad bin Zayd. And by Bukhari, in the Book of Wudu, chapter on what to say upon entering the toilet, hadith 142, from the narration of Abdul-Aziz bin Suhayb, through the same chain.