Recently, I had to change the “Password Never Expires” flag for some users on Windows systems.
In the past, there used to be a netuser tool available from WindowsITPro where you could use the pwnexp flag to set or clear that flag.
That tool seems to be vanished, so I was searching for alternatives.
Most alternatives I found depend on some kind of scripting, or the use of the WMIC WMI command line interface: that was “out” because this particular setup is running on Windows XP Embedded, which is trimmed down very much.
The only C# example I found was on CodeProject, but it does
- not take into account the existing flags correctly,
- have hard coded literals without any references where they are from,
- use bit flag arithmetic without letting the C# compiler do its magic with enums,
- use a call to the deprecated InvokeGet method,
- use the Invoke(“Put”, … way of calling that so many people use (which actually should have been an – also deprecated – InvokeSet method),
- use COM Interop
Hence the solution below:
C#, with the proper ADS_USER_FLAG_ENUM enum from the MSDN documentation and no COM Interop, it also moves all literals to constants.





