Reputation: 149
I have this two commands to get SID useraccount
wmic useraccount where name='%username%' get sid | findstr /b /C:"S-1" > file.txt
or
for /F "tokens=2 delims=," %f in ('whoami /user /FO CSV /NH') do echo %~f > file.txt
both cases (file.txt) contain blank spaces/empty lines. I try to remove with
findstr /r /v "^$"
But it is impossible. How can remove all blank spaces/empty lines to get only SID number
Upvotes: 4
Views: 1073
Reputation: 38708
Here's how I'd do it, WMIC:
for /f "skip=1" %a in ('"wmic useraccount where name='%username%' get sid"') do @for %b in (%a) do @(>file.txt echo(%b)
and with WhoAmI
for /f tokens^=3^ delims^=^" %a in ('whoami /user /FO CSV /NH') do @(>file.txt echo(%a)
both in the command line to match the question (ignoring the batch-file tag).
Upvotes: 0
Reputation: 70941
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "skip=2 tokens=2 delims=," %%a in ('
wmic path win32_useraccount
where name^="%username%"
get sid^,status /format:csv
') do (
>"file.txt" <nul set /p"=%%a"
)
That will skip headers, include an additional field so the sid
is the second field in the record (in csv
format the node
field gets included) so we can avoid an ending carriage return in the output of the wmic
command (each line ends in CR+CR+LF
) and the output is sent to the file using a set /p
command with the input stream reading from nul
device. This way the output to the file will not include the ending CR+LF
that is included with echo
The same code for in the whoami
version
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims=," %%a in ('
whoami /user /FO CSV /NH
') do (
>"file.txt" <nul set /p"=%%~a"
)
Upvotes: 2
Reputation: 18847
This batch file can did the trick :
@echo off
for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
if not "%%a"=="SID" (
set myvar=%%a
goto :loop_end
)
)
:loop_end
echo SID=%myvar%
echo %myvar% > SID_%username%.txt
pause>nul
Start "" SID_%username%.txt
For the second method of whoami /user /FO CSV /NH
,you can take a look at this :
Get SID for current logged in domain user
Upvotes: 2