I think that if he is unaware that he is excommunicated and still is in a position to administer the Sacraments, then yes, an excommunicated priest would be giving valid Sacraments.
How could this be? Suppose he believed abortion was an acceptable thing to do and helped more than one gal in tight spot “get help,” then he’d be excommunicated. Or suppose he broke the seal of Confession by gossiping about things he heard in the Confessional. He’d be automatically excommunicated in that instance too. Suppose he told a young man to have “safe” sex in the Confessional. He’d be gonners in that case too. There are things one can think and do that incur automatic excommunication and until one repents of them, one has no relief from the Sacraments.
Formal Excommunication that happens in writing, signed, sealed and delivered usually is enough of a push out the door a priest so he will no longer try to administer any Sacraments. There are those who know they are excommunicated and don’t care and still administer the Sacraments. But that has happened to those whose excommunication was made publically and they refused to stop administering the Sacraments, i.e. the SSPX folks who even went so far as to Ordain. They handed out a mess of illicit Sacraments.
Sacraments CAN be illicit but still valid. Take for example a bad Confessor administering a sloppy job in the Confessional. It may illicit, but it is still valid.
So I may have not said it as well as some here can and do, but a priest may administer Sacraments while being in a state of Excommunication BUT they’d be illicit but not invalid.
But please don’t take my word for it. I’m no expert and all of this is simple lay scholarship on my part.
Glenda
P.S. If a priest has been formally excommunicated, he’s had his faculties removed too. That means he isn’t to try and administer ANY Sacraments including Baptism UNLESS there is actually danger of death i.e. the Titanic is sinking and he’s on it. Until he’s actually laicized, he can give Sacraments.