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.
Related
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
FOR /F "tokens=3 delims= " %i IN (query session | FINDSTR /i "console") DO set "ID=%i"
I am getting an error | was unexpected at this time.
Two very simple mistakes to avoid.
First by reading For /? use 'single quotes' for commands.
Secondly ^escape the |
FOR /F "tokens=3 delims= " %i IN ('query session ^| FINDSTR /i "console"') DO set "ID=%i"
Reminder as mentioned by #aschipfl in comment use doubled %% for both of your i's in a batch file or file.cmd
You should always read the usage information for a command utility before you use it. Had you done so, you would have noted that as you are specifically trying to isolate a line with the session name console, you could have used query session console instead of the less robust query session | FINDSTR /i "console". Of course, using the more appropriate command would mean that you do not have any issue with a horizontal bar, (pipe).
If you wanted, you could also skip the first, (header), line, and if you are certain, that your ID will always be the third whitespace separated token, you could then use:
From the Command Prompt, (cmd):
For /F "Skip=1 Tokens=3" %G In ('%SystemRoot%\System32\query.exe Session Console 2^>NUL') Do #Set "ID=%G"
Or from a batch file, (batch-file):
#For /F "Skip=1 Tokens=3" %%G In ('%SystemRoot%\System32\query.exe Session Console 2^>NUL') Do #Set "ID=%%G"
I am looking to implement a simple batch file that will rename the current local profile folder, backup registry keys then delete profile list SID keys.
Therefore allowing the computer to create a new local profile that is not temporary.
Where %U% is the account name
set a=wmic useraccount where name="%U%" get sid /value
and so when I execute the below(Where %a% is the above command):
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%a% /f
it interprets it as:
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\wmic useraccount where name="%U%" get sid /value"
But I want:
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\SID=S-1-5-21-3519583588-1143172139-1479499458-1001"
If I call %a%, it displays
SID=S-1-5-21-3519583588-1143172139-1479499458-1001
and if I echo %a%, it displays
wmic useraccount where name="%U%" get sid
If I just enter %a%, it displays
SID=S-1-5-21-3519583588-1143172139-1479499458-1001
Just an explanation for why this happens would be great.
set does not have the built-in ability to execute a command and store the result, as you intend with the line:
set a=wmic useraccount where name="%U%" get sid /value
Instead, a simple hack is:
#echo off
for /f %%A in ('wmic useraccount where "name='%USERNAME%'" get sid /value ^| findstr SID') do ( set %%A )
echo The SID is %SID%
After that, you should be able to:
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%" /f
Unlike Unix where you use backticks for an expression which evaluates to the stdout of an external command there is no such syntax in batches. What you need to do instead is to use a for loop over the output lines to assign it to a variable, like discussed here.
for /f "delims=" %%i in ('wmic...') do set A=%%i
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 ....
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