4

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

Community
  • 1
  • 1
BrianC
  • 149
  • 1
  • 2
  • 9

3 Answers3

2

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

Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
2
@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"
    )
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • thanks works with whoami. wmic generates an error because csv format [bug](http://stackoverflow.com/questions/9673057/wmic-error-invalid-xsl-format-in-windows7) – BrianC Oct 18 '16 at 12:13
0

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).

Compo
  • 36,585
  • 5
  • 27
  • 39
  • sorry. this doesn't work. I recommend you first try to verify commands that work before publishing – BrianC Oct 31 '16 at 15:56
  • Thankyou for pointing out that there was a typo, it would have helped had you told me which of the two lines of code contained it though. I cannot test most of the code I provide as I rarely have access to a Windows PC. – Compo Oct 31 '16 at 16:03
  • 1
    bad quality code. You should test it before publishing – BrianC Oct 31 '16 at 20:27