Anas bin Malik reported,

Whenever the Prophet ﷺ entered the toilet, he would say, "Allah, I seek refuge in You from male and female evil spirits."Ibn Ararah followed the same wording from Shuba. Ghundar reported from Shuba with the wording, "Whenever he came to the toilet." Musa reported from Hammad with the wording, "Whenever he entered." Saeed bin Zayd narrated to us from Abdul-Aziz with the wording, "Whenever he intended to enter."

Bukhari 142

Sahih

حدثنا آدم قال حدثنا شعبة عن عبد العزيز بن صهيب قال سمعت

أنسا يقول

كان النبي ﷺ إذا دخل الخلاء قال اللهم إني أعوذ بك من الخبث والخبائث تابعه ابن عرعرة عن شعبة وقال غندر عن شعبة إذا أتى الخلاء وقال موسى عن حماد إذا دخل وقال سعيد بن زيد حدثنا عبد العزيز إذا أراد أن يدخل

البخاري 142صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Wudu
Chapter
What to say when using the lavatory
Numbering
Darussalam 142
Bukhari 142