Whenever the Prophet ﷺ went to answer the call of nature, he used to say, "Allah-umma inni aʿudhu bika minal khubuthi wal khaba'ith i.e. O Allah, I seek Refuge with You from all offensive and wicked things (evil deeds and evil spirits).
سَمِعْتُ أَنَسًا يَقُولُ كَانَ النَّبِيُّ ﷺ إِذَا دَخَلَ الْخَلاَءَ قَالَ اللَّهُمَّ إِنِّي أَعُوذُ بِكَ مِنَ الْخُبُثِ وَالْخَبَائِثِ