Monday, December 21, 2015

Testing for membership in a certain domain in Windows batch script

If you want to test for the current user's membership in a certain domain in a Windows batch script, the whoami utility has you covered. Pipe it into findstr /i "domainname\\" to test whether the output includes your domain's name. The double backslash is there to represent a single backslash (escaping), the one that separates the domain component from the username component.

findstr sets the errorlevel to 1 if the string is not found or to 0 if it is. So, you can use an if statement to act on the domain membership or lack thereof:

whoami | findstr /i "kingdom\\"
if errorlevel 1 (
 REM do some non-domain business
) else (
 REM do some domain business
)

No comments:

Post a Comment