Method 1: Using CSVDE and Blat (Third-party email tool)
First, you'll need to download and install
Blat (a command-line email tool):
batch
@echo off
setlocal enabledelayedexpansion
:: Set variables
set DOMAIN=yourdomain.com
set SMTP_SERVER=smtp.yourdomain.com
set FROM_EMAIL=
report@yourdomain.com
set TO_EMAIL=
recipient@yourdomain.com
set REPORT_FILE=dave_users_report.txt
set CSV_FILE=dave_users.csv
echo Generating report for users named Dave...
:: Extract users with name containing "Dave" using CSVDE
csvde -f %CSV_FILE% -r "(&(objectCategory=person)(objectClass=user)(|(name=*Dave*)(sn=Dave*)(givenName=Dave*)))" -l "name,mail,samAccountName,displayName"
:: Create formatted table
echo User Report - All Users Named Dave > %REPORT_FILE%
echo ================================= >> %REPORT_FILE%
echo. >> %REPORT_FILE%
echo "Display Name","Username","Email Address" >> %REPORT_FILE%
echo -------------------------,----------------,------------------------- >> %REPORT_FILE%
:: Parse CSV and format as table
for /f "skip=1 tokens=1,2,3,4 delims=," %%a in (%CSV_FILE%) do (
set "displayName=%%a"
set "email=%%b"
set "username=%%c"
set "name=%%d"
:: Remove quotes
set "displayName=!displayName:"=!"
set "email=!email:"=!"
set "username=!username:"=!"
set "name=!name:"=!"
:: Only include if email exists and name contains Dave
if not "!email!"=="" (
echo "!displayName!","!username!","!email!" >> %REPORT_FILE%
)
)
:: Send email using Blat
blat %REPORT_FILE% -to %TO_EMAIL% -f %FROM_EMAIL% -server %SMTP_SERVER% -s "Domain Users Report - Users Named Dave" -body "Attached is the report of all users named Dave in the domain."
:: Clean up temporary files
del %CSV_FILE%
echo Report generated and sent successfully!
echo Report file: %REPORT_FILE%
Method 2: Using Net Command (Simpler approach)
batch
@echo off
setlocal enabledelayedexpansion
:: Set variables
set DOMAIN=yourdomain.com
set SMTP_SERVER=smtp.yourdomain.com
set FROM_EMAIL=
report@yourdomain.com
set TO_EMAIL=
recipient@yourdomain.com
set REPORT_FILE=dave_users_report.txt
echo Generating report for users named Dave...
:: Create report header
echo User Report - All Users Named Dave > %REPORT_FILE%
echo ================================= >> %REPORT_FILE%
echo. >> %REPORT_FILE%
echo "Display Name","Username","Email Address" >> %REPORT_FILE%
echo -------------------------,----------------,------------------------- >> %REPORT_FILE%
:: Get users and attempt to extract email (limited by net command)
net user /domain | findstr /i "dave" > temp_users.txt
for /f "tokens=1 delims=" %%a in (temp_users.txt) do (
set "userline=%%a"
set "username=!userline:~0,20!"
set "fullname=!userline:~20,30!"
:: Trim spaces
for /f "tokens=*" %%i in ("!username!") do set "username=%%i"
for /f "tokens=*" %%i in ("!fullname!") do set "fullname=%%i"
:: Get user info to try to find email
net user "!username!" /domain | findstr /i "comment" > temp_info.txt
set "email="
for /f "tokens=1,*" %%x in (temp_info.txt) do (
set "email=%%y"
)
if not "!email!"=="" (
echo "!fullname!","!username!","!email!" >> %REPORT_FILE%
) else (
echo "!fullname!","!username!","Not Available" >> %REPORT_FILE%
)
)
:: Send email (using Blat)
blat %REPORT_FILE% -to %TO_EMAIL% -f %FROM_EMAIL% -server %SMTP_SERVER% -s "Domain Users Named Dave Report" -body "Please find the attached report of users named Dave in the domain."
:: Clean up
del temp_users.txt
del temp_info.txt 2>nul
echo Report completed: %REPORT_FILE%
Method 3: Using WMIC (Alternative approach)
batch
@echo off
setlocal enabledelayedexpansion
set REPORT_FILE=dave_users_report.txt
set SMTP_SERVER=smtp.yourdomain.com
set FROM_EMAIL=
report@yourdomain.com
set TO_EMAIL=
recipient@yourdomain.com
echo "Name","Username","Email" > %REPORT_FILE%
echo -------------------------,----------------,------------------------- >> %REPORT_FILE%
:: Use WMIC to get user information
wmic useraccount where "name like '%%dave%%'" get name,sid /value > temp_wmic.txt
for /f "tokens=1,2 delims==" %%a in (temp_wmic.txt) do (
if "%%a"=="Name" set "username=%%b"
if "%%a"=="SID" (
set "sid=%%b"
:: This is where you'd typically look up email, but WMIC doesn't provide it directly
echo "Dave User","!username!","See Active Directory" >> %REPORT_FILE%
)
)
:: Send email
blat %REPORT_FILE% -to %TO_EMAIL% -f %FROM_EMAIL% -server %SMTP_SERVER% -s "Dave Users Report" -body "Report of users named Dave"
del temp_wmic.txt
echo Report sent!
Setup Instructions:
- Install Blat:
- Configure the script:
- Replace email server details
- Replace domain information
- Replace email addresses
- Run the script as Domain Administrator for best results
Notes:
- Method 1 (CSVDE) is most reliable but requires proper AD permissions
- Method 2 (Net Command) is simpler but has limited email extraction
- The net user command doesn't directly provide email addresses, so you might need to rely on the "Comment" field or use CSVDE
- For production use, consider storing email credentials securely if authentication is required
Would you like me to modify any part of this solution or help with the Blat configuration?