Wmic useraccount SID spaces / blank lines

I have these two commands to get useraccount sid

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 spaces / blank lines. I am trying to remove using

findstr /r /v "^$"

But this is unreal. How to remove all spaces / blank lines to get only SID number

+4
source share
3 answers
@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"
    )

, , sid - ( csv node), wmic ( CR+CR+LF), set /p nul. , CR+LF, echo

whoami

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=2 delims=," %%a in ('
        whoami /user /FO CSV /NH
    ') do (
        >"file.txt" <nul set /p"=%%~a"
    )
+2

:

@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

whoami /user /FO CSV /NH :

SID

+1

, WMIC:

for /f "skip=1" %a in ('"wmic useraccount where name='%username%' get sid"') do @for %b in (%a) do @(>file.txt echo(%b)

WhoAmI

for /f tokens^=3^ delims^=^" %a in ('whoami /user /FO CSV /NH') do @(>file.txt echo(%a)

both on the command line and in the question (ignoring the batch file tag).

0
source

Source: https://habr.com/ru/post/1658047/


All Articles