CMD batch to list domain memberships line by line

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi All,
Running this CMD script NET USER /DO (USERNAME) shown domain memberships of user in two column. How to rewrite the CMD script to list the memberships line by line

netuser.PNG
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maybe this may help.. Sort net group output in one column

Otherwise, I have this VBA macro. You will need to add a reference to "Windows Script Host Object Model".
Don't forget to change username for strUserName = "username_here"
Will print out to active sheet column A, starting from row 1.
I am not in a domain and only global group is *None, so I could not perform much of a testing.

VBA Code:
Option Explicit
'Option Private Module
Option Compare Text


Sub test()
    
    Dim oShell As WshShell
    Dim oExec As WshExec
    
    Dim str As String
    Dim strReturn As String
    Dim strUserName as String

    strUserName = "username_here"
    str = "net user /DO " & strUserName
    
    Set oShell = New WshShell
    Set oExec = oShell.Exec("cmd.exe")
    
    On Error Resume Next
    
    oExec.StdIn.WriteLine (str)
    oExec.StdIn.Close
    
    strReturn = oExec.StdOut.ReadAll

    Debug.Print strReturn
    
    On Error GoTo 0
    
    Err.Clear
    
    If Not (oExec Is Nothing) Then Set oShell = Nothing
    If Not (oShell Is Nothing) Then Set oShell = Nothing
    
    strReturn = Split(strReturn, "Global Group memberships")(1)
    strReturn = Split(strReturn, "The command")(0)
    strReturn = Replace(strReturn, vbCrLf, "")
    
    Dim arrSplit As Variant

    arrSplit = Split(strReturn, "*")
    
    Dim i As Long
    
    For i = 0 To UBound(arrSplit)
        ActiveSheet.Range("A1").Offset(i).Value = Trim(arrSplit(i))
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top