Remote GPResult Batch File

Posted on 2012-08-27
Last Modified: 2012-08-27
I have a batch file that contains:

gpresult /s %1 /user %2 /h report.htm /f

How can I change this so that /user %2 is the current logged in user of the remote machine?
Question by:Joseph Moody
    LVL 13

    Expert Comment

    Windows XP:


    Windows 7

    LVL 21

    Author Comment

    by:Joseph Moody
    The %username% inputs the current logged in user of my machine. How can I have it give me the current logged in user of the remote machine?
    LVL 82

    Accepted Solution

    It's not that easy, because there may be several accounts logged on on the remote machine. Download Microsoft's PsLoggedOn ( and put it into the same folder as the script below. The script will query for logged on users on the remote machine, and give you a menu to pick form if more than user is logged on. As before, you can still pass it the user name in the command line as second argument:
    @echo off
    setlocal enabledelayedexpansion
    set ReportFile=report.html
    if "%~1"=="" (
    	echo Syntax: %~nx0 Computer [Username]
    	goto :eof
    set Computer=%~1
    if not "%~2"=="" (
    	set User=%~2
    	goto GPResult
    set User=
    set /a UserCount = 0
    for /f "skip=1" %%a in ('psloggedon.exe -x -l -accepteula \\%Computer% 2^>NUL') do (
    	set /a UserCount += 1
    	set User[!UserCount!]=%%~a
    if "%UserCount%"=="0" (
    	echo No users sessions found on %Computer%.
    	goto :eof
    if "%UserCount%"=="1" (
    	set User=%User[1]%
    	goto GPResult
    set User=
    echo More than one user is logged on; please choose one:
    for /l %%i in (1, 1, %UserCount%) do (
    	echo %%i	!User[%%i]!
    set /p ID=^>
    set User=!User[%ID%]!
    if "%User%"=="" goto :eof
    gpresult.exe /s %Computer% /user %User% /h "%ReportFile%" /f
    start "" "%ReportFile%"

    Open in new window

    LVL 21

    Author Closing Comment

    by:Joseph Moody
    If I could give you 50,000 points, I would! This is awesome!!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
    To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now