To do this create a custom script or use system-specific tools.
You may configure a PowerShell script or, use the tsdiscon.exe command to disconnect from the session and lock the screen. To do this make a batch file.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/tsdiscon
@echo off
tsdiscon
timeout /t 3 /nobreak
rundll32.exe user32.dll,LockWorkStation