Print the full name of user in batch - windows

I need to print the full name of the current user in the console, I have this code that does what I need but only partially
NET USER %username% /DOMAIN | FIND /I "Full name" >tmp.txt
set /p VAR=<tmp.txt
echo %VAR%
del tmp.txt
Because the result of the print is for example "Full Name Juan Perez"
And the only thing I need is "Juan Perez"
Is there a way to do what I need?

Theoretically,
#ECHO OFF
SETLOCAL
FOR /f "tokens=2*delims= " %%b IN ('NET USER %username% /DOMAIN 2^>nul ^|find /I "Full name"') DO SET "var=%%c"
ECHO Full name : "%var%"
GOTO :EOF
but NET USER %username% /DOMAIN returns an error for me.
The 2^>nul redirects errors. the ^ before > and | tells cmd the character is part of the '-enclosed command to be executed, not of the for itself.
Documentation for for can be found by executing for /? from the prompt, or reading thousands of examples on SO

Related

How to logoff all users on windows from command line as a domain administrator

I'm running automation testing on Windows (windows 10 and 2012), and one of the requirements of the automation is that ALL users need to be logged off. I have a chance to do this after deployment. I kind of see this page give an answer, but after I tried query session, I see it gives even services and rdp-tcp sessions... but I don't want to stop any service...
Any suggestion?
This solution is based on the previous answer but this one will close all sessions (even disconnected sessions). Unfortunately, the text format of each line returned by the 'query session' command can be difficult to parse. As you can see in the next screenshot, the username may be empty and in this example, if you use tokens=3, you will get the username instead of the ID for the user compil.
The problem is that you cant logoff a disconnected user with his username. To circumvent this problem, we use 2 for loops to get the session name and id (depending on the line format), and then we keep only the numeric values to send the logoff command and logoff also the disconnected sessions.
#echo off
for /f "skip=2 tokens=2,3 delims= " %%a in ('query session') DO (
echo %%a|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%a
)
echo %%b|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%b
)
)
The page you linked has the correct answer. Except that in Windows 2012 / 10 you should use skip=2 instead of 1. This way you will skip the 'services' line.
So your batch file will look like this:
query session >session.txt
for /f "skip=2 tokens=3," %%i in (session.txt) DO logoff %%i
del session.txt
We use this to log off all rdp sessions with the exception of the administrator. Could be tweaked as needed.
#echo off
:: Log off Active Users
query session | findstr "rdp, Active" | findstr /V "dministrator" >sessionActive.txt
for /f "tokens=3" %%i in (sessionActive.txt) DO logoff %%i
del sessionActive.txt
:: Log off Disconnected Users
query session | findstr "Disc" | findstr /V "dministrator, services" >sessionDisc.txt
for /f "tokens=2" %%i in (sessionDisc.txt) DO logoff %%i
del sessionDisc.txt
Note: "file.txt" contain computer name for each line.
for /f "tokens=*" %%i in ("*\file.txt") do (
psexec \\%%i -u domain\user -p password logoff console
)
Before using this code, you must download PsTools by this link:
https://download.sysinternals.com/files/PSTools.zip
https://learn.microsoft.com/vi-vn/sysinternals/downloads/psexec
Then extract PsTools archive => Copy all file => Paste to "*:\Windows\System32" => Installation Complete
use eol=> to skip active user
#echo off
query session > session.txt
::quser > session.txt
for /f "skip=2 eol=> tokens=2,3 delims= " %%G in (session.txt) DO (
echo %%G|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%G & echo logoff disconnect user %%G
)
echo %%H|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%H & echo logoff active user %%H
)
)
del session.txt

Issues with spaces in FOR loop variable - batch script

I'm writing a batch script that will use a WMIC command to get a list of all groups on a Windows machine, get the group info by using net localgroup <groupname>, and then write the info to an output file. Here is what I have:
for /f "skip=1" %%a in ('"wmic group get name"') do net localgroup %%a >> "%OUTPUTFILEPATH%" 2> nul && echo. >> "%OUTPUTFILEPATH%" && echo ^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^= >> "%OUTPUTFILEPATH%" && echo. >> "%OUTPUTFILEPATH%"
The issue I am having seems to be with getting quotes around the %%a variable in do net localgroup %%a because it outputs the info for groups like Administrators just fine but when it gets to a group name like Remote Desktop Users, it fails to return the info for the group.
In other words, this is what seems to be happening in the FOR loop:
net localgroup Administrators
net localgroup Remote Desktop Administrators
The first operation is successful. The second is not. Obviously, there need to be quotes around Remote Desktop Administrators in order for it to be seen as a single argument but I can't quite figure out how to get this to happen for my %%a FOR loop variable.
I have tried putting quotes around "%%a" and '%%a'. This doesn't help.
Any input would be greatly appreciated.
REM
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "OUTPUTFILEPATH=u:\ofp.txt"
DEL /F /Q "%outputfilepath%"
for /f "skip=1delims=" %%a in ('wmic group get name') do (
SET "group=%%a"
CALL :loptrail
IF DEFINED group (
net localgroup "!group!" >> "%OUTPUTFILEPATH%" 2> NUL
echo.>> "%OUTPUTFILEPATH%"
echo ==========================================>> "%OUTPUTFILEPATH%"
echo.>> "%OUTPUTFILEPATH%"
)
)
GOTO :EOF
:loptrail
SET "group=%group:~0,-1%"
IF "%group:~-1%"==" " GOTO loptrail
GOTO :eof
The issue is that wmic output is unicode and %%a requires delims= else it returns the first [implicit-space]-delimited token.
Unfortunately, this means that %%a contains trailing spaces, which net appears to disapprove of.
Since batch does not allow a metavariable to be substringed, you need to put it into a common environment variable and invoke enabledelayedexpansion.
The value in group appears to be terminated by an extra NULL, so the :loptrail routine first arbitrarily removes that last character, then any remaining trailing spaces.
The very last line of wmic output will generate a forlorn empty group so the net processing proceeds only if group is non-empty.
You need to use either tokens=* as suggested by Carlos GutiƩrrez or even better delims= on FOR to avoid splitting up the line into tokens based on spaces and tabs.
It is of course additionally necessary to enclose the group name in double quotes when any group name contains 1 or more spaces.
But the main problem here is that wmic outputs information in Unicode with UTF-16 Little Endian encoding which command FOR cannot parse right directly. A workaround for this issue is redirecting output of wmic into a temporary text file and using command type to get contents in ASCII/ANSI/OEM, i.e. one byte per character. See the answers on How to correct variable overwriting misbehavior when parsing output for details.
But there is one more problem here as wmic outputs the group names with trailing spaces. Those trailing spaces must be additionally removed before group name is passed as parameter to console application net enclosed in double quotes.
#echo off
setlocal EnableDelayedExpansion
set "OutputFile=%USERPROFILE%\Desktop\LocalGroupInfo.txt"
del "%OutputFile%" 2>nul
%SystemRoot%\System32\wbem\wmic.exe group get name >"%Temp%\%~n0.tmp"
for /f "skip=1 delims=" %%a in ('type "%Temp%\%~n0.tmp"') do (
set "GroupName=%%a"
call :RemoveTrailingSpaces
%SystemRoot%\System32\net.exe localgroup "!GroupName!" >>"%OutputFile%" 2>nul
if not errorlevel 1 (
echo.>>"%OutputFile%"
echo ==========================================>>"%OutputFile%"
echo.>>"%OutputFile%"
)
)
del "%Temp%\%~n0.tmp"
endlocal
:RemoveTrailingSpaces
if not "%GroupName:~-1%" == " " exit /B
set "GroupName=%GroupName:~0,-1%"
goto RemoveTrailingSpaces
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
net localgroup /?
set /?
setlocal /?
type /?
wmic group get /?
Try:
for /f "tokens=*" %%a in ('"wmic group get name"') do ....

Full name to variable in CMD

this should be pretty basic, but I can't figure it out.
I trying to set the full name of the user to a variable to use further in the my batch script.
I thought it should be something like this:
SET VAR=NET USER %username% /DOMAIN | FIND /I "Full name";
echo "%VAR%"
The NET USER %username% /DOMAIN | FIND /I "Full name" works on its own, but not when I try to set it to a variable.
Maybe this is more a general question..
You can use a temporary file or for /f to achieve this:
temporary file solution:
NET USER %username% /DOMAIN | FIND /I "Full name" >tmp.txt
set /p VAR=<tmp.txt
echo %VAR%
del tmp.txt
for /f solution:
for /f "tokens=*" %i in ('NET USER %username% /DOMAIN ^| FIND /I "Full name"') do set VAR=%i
Note:
Replace % with %% if using the above command in a batch file.
Use "tokens=*" to match all of the output from the command
^ is used because the | (pipe) must be escaped.
You could use the already specified %username% and %userdomain%.
Type set for a list of values.
Type set /? for a list of dynamic variables.

logoff user with .cmd file on destop

I have a code :
#echo off
rem :: Get session ID
for /f "tokens=3" %%I in ('qwinsta /server:10.10.100.1 ^| find /i " %username% "') do (set _ID=%%I)
rem :: Logoff user
logoff %_ID% /server:10.10.100.1
It works if I type it into command line, but when I take this code and make .cmd file and put on my desktop nothing happends. Don't know why when It works. I also tried put (ping localhost -n 1 -w 5000) to give time to set _ID variable, but didn't help. What could be a problem? Thank you for your answers.
Try this and look at the last line for an error.
#echo off
rem :: Get session ID
for /f "tokens=3" %%I in ('qwinsta /server:10.10.100.1 ^| find /i " %username% "') do (set _ID=%%I)
rem :: Logoff user
echo logoff %_ID% /server:10.10.100.1
pause

batch: ignore last line of an output

I have a pretty basic script that echos local administrator accounts. My goal is to get rid of all of the header/footer information.
So far I have:
FOR /F "skip=6" %%G IN ('net localgroup administrators') DO echo %%G
Which echos:
Administrator
MyName
The
"The" being the first word in the footer: "The command completed successfully."
So I'd like to get rid of "The" but I understand that I may have to restructure the entire script, which is fine. I have tried saving to a variable %str% but you can't set multi-line variables. Also, using a txt file as a buffer is not an option.
Any input?
I can think of two simple solutions:
FOR /F "skip=6" %%G IN ('net localgroup administrators') DO if %%G neq The echo %%G
or
FOR /F "skip=6" %%G IN ('net localgroup administrators ^| findstr /vb The') DO echo %%G
I suppose one could argue a user name could be "The", in which case you can be more precise with the filter:
FOR /F "skip=6" %%G IN ('net localgroup administrators ^| findstr /vc:"The command completed successfully."') DO echo %%G

Resources