Local User Report | ForEach-Loop & Remote questions

Hi All,

Working on a script to report the local user account information from a system and display them in a HTML report or CSV.

I’m studying For-Each loops and want to loop through my Various Commands and append them to a table.

Command 1) Hostname

Command 2) (net localgroup administrators).where({$_ -match ‘-{79}’},‘skipuntil’) -notmatch ‘-{79}|The command completed’

Command 3) Get-WmiObject -Class Win32_UserAccount -Filter “LocalAccount=‘True’” | Select name -expandproperty name

Command 4) Get-ChildItem -Path C:\Users\ | select-object Name

  • Q1. Is anyone able to add my commands to an example for me to play with?
  • Q2. I have to run this against computers (200+) within a domain. There are only a few systems with WSMan enabled, I will need to look at that on a separate note. If WS Man was enabled across all systems, would invoke-command would be the best option?
  • As my script stands, I can Enter-PSSession and manually RUN the script against a remote PC, but I want to do this against ALL my computers.

    $A = hostname

    $B = (net localgroup administrators).where({$_ -match ‘-{79}’},‘skipuntil’) -notmatch ‘-{79}|The command completed’

    $C = Get-WmiObject -Class Win32_UserAccount -Filter “LocalAccount=‘True’” | Select name -expandproperty name

    $D = Get-ChildItem -Path C:\Users\ | select-object Name

    $out = new-object psobject
    $out | add-member noteproperty Node $a
    $out | add-member noteproperty Administrators $b
    $out | add-member noteproperty LocalAccounts $c
    $out | add-member noteproperty Profiles $d
    write-output $out

    Output:
    https://static.spiceworks.com/shared/post/0025/5747/PSResult.png

    Really often you are not the first with a given task. Did you try to search for a solution? If I search in Google for “powershell wmi local administrators group” I get as the first hit “Get remote machine members of Local Administrator group
    So no need to reinvent the wheel. :wink:

    … and also really often looking at other peoples code is indeed a great way to learn somthing about Powershell.