[Android Studio] thienha1:
Any AVD installed? Yes

Then I redownload your script and command again and what i got

Command:

PS C:\Users\Zed\Downloads\rootAVD-master> .\rootAVD.bat ListAllAVDs

Result:

rootAVD A Script to root AVD by NewBit XDA

Usage: rootAVD [DIR/ramdisk.img] [OPTIONS] | [EXTRA_CMDS]
or: rootAVD [ARGUMENTS]

Arguments:
ListAllAVDs Lists Command Examples for ALL installed AVDs

EnvFixTask Requires Additional Setup fix
- construct Magisk Environment manual
- only works with an already Magisk patched ramdisk.img
- without [DIR/ramdisk.img] [OPTIONS] [PATCHFSTAB]
- needed since Android 12 (S) rev.1
- not needed anymore since Android 12 (S) API 31 and Magisk Alpha
- Grant Shell Su Permissions will pop up a few times
- the AVD will reboot automatically

InstallApps Just install all APKs placed in the Apps folder

Main operation mode:
DIR a path to an AVD system-image
- must always be the 1st Argument after rootAVD

ADB Path | Ramdisk DIR:
[M]ac/Darwin: export PATH=~/Library/Android/sdk/platform-tools:\$PATH
~/Library/Android/sdk/system-images/android-\$API/google_apis_playstore/x86_64/

[L]inux: export PATH=~/Android/Sdk/platform-tools:\$PATH
~/Android/Sdk/system-images/android-\$API/google_apis_playstore/x86_64/

[W]indows: set PATH=%LOCALAPPDATA%\Android\Sdk\platform-tools;%PATH%
%LOCALAPPDATA%\Android\Sdk\system-images\android-$API\google_apis_playstore\x86_64\

$API: 25,29,30,S,etc.

Except for EnvFixTask, ramdisk.img must be untouched (stock).

Options:
restore restore all existing .backup files, but doesn't delete them
- the AVD doesn't need to be running
- no other Argument after will be processed

InstallKernelModules install custom build kernel and its modules into ramdisk.img
- kernel (bzImage) and its modules (initramfs.img) are inside rootAVD
- both files will be deleted after installation

InstallPrebuiltKernelModules download and install an AOSP prebuilt kernel and its modules into ramdisk.img
- similar to InstallKernelModules, but the AVD needs to be online

Options are exclusive, only one at the time will be processed.

Extra Commands:
DEBUG Debugging Mode, prevents rootAVD to pull back any patched file

PATCHFSTAB fstab.ranchu will get patched to automount Block Devices like /dev/block/sda1
- other entries can be added in the script as well
- a custom build Kernel might be necessary

GetUSBHPmodZ The USB HOST Permissions Module Zip will be downloaded into /sdcard/Download

Extra Commands can be combined, there is no particular order.

Notes: rootAVD will
- always create .backup files of ramdisk.img and kernel-ranchu
- replace both when done patching
- show a Menu, to choose the Magisk Version (Stable || Canary || Alpha), if the AVD is online
- make the choosen Magisk Version to its local
- install all APKs placed in the Apps folder

Command Examples:
rootAVD.bat
rootAVD.bat ListAllAVDs
rootAVD.bat EnvFixTask
rootAVD.bat InstallApps

rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img
rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img DEBUG PATCHFSTAB GetUSBHPmodZ
rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img restore
rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img InstallKernelModules
rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img InstallPrebuiltKernelModules
rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img InstallPrebuiltKernelModules GetUSBHPmodZ PATCHFSTAB DEBUG

Command 2:

.\rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-28\google_apis\x86\ramdisk.img

Result 2:

[*] Set Directorys
[-] Test if ADB SHELL is working
[!] ADB is not in your Path, try to
set PATH=%LOCALAPPDATA%\Android\Sdk\platform-tools;%PATH%

Also AVD still running

In the testADB routine as you mention, where can i set PATH like this info?

:TestADB
SetLocal EnableDelayedExpansion
set HOME=%LOCALAPPDATA%\
set ADB_DIR_W=Android\Sdk\platform-tools\
set ADB_DIR=""
set ADB_EX=""

echo [-] Test if ADB SHELL is working

set ADBWORKS=
adb shell -n echo true > tmpFile 2>&1
set /P ADBWORKS=<tmpFile
del tmpFile

if "%ADBWORKS%" == "true" (
echo [-] ADB connectoin possible
) else (

echo.%ADBWORKS%| FIND /I "offline">Nul && (
echo [^^!] ADB device is offline
echo [*] no ADB connection possible
call :_Exit 2> nul
)

echo.%ADBWORKS%| FIND /I "unauthorized">Nul && (
echo [^^!] %ADBWORKS%
echo [*] no ADB connection possible
call :_Exit 2> nul
)

echo.%ADBWORKS%| FIND /I "recognized">Nul && (
if exist %HOME%%ADB_DIR_W% (
set ADB_DIR=%ADB_DIR_W%
) else (
echo [^^!] ADB not found, please install platform-tools and add it to your %%PATH%%
call :_Exit 2> nul
)

for /f "delims=" %%i in ('dir %HOME%%ADB_DIR%adb.exe /s /b /a-d') do (
set ADB_EX=%%i
)

if !ADB_EX! == "" (
echo [^^!] ADB binary not found in %%LOCALAPPDATA%%\%ADB_DIR%
call :_Exit 2> nul
)

echo [^^!] ADB is not in your Path, try to
echo set PATH=%%LOCALAPPDATA%%\Android\Sdk\platform-tools;%%PATH%%
call :_Exit 2> nul
)

echo.%ADBWORKS%| FIND /I "error">Nul && (
echo [^^!] %ADBWORKS%
echo [*] no ADB connection possible
call :_Exit 2> nul
)
call :_Exit 2> nul
)
ENDLOCAL
exit /B 0