2

I work in an environment with a mix of older and newer Windows components on isolated networks. To try and make a certain task easier, I worked on a list of PowerShell commands to create a single text file with specific input for all members of a local groups using "net localgroup" with the idea it could be repurposed using "net group".

cd C:\Users\MyName\Desktop\
mkdir test
cd test
$a = pwd
net localgroup | out-file $a\net_localgroup_list.txt

# removes white space and non-relevant lines to just provide the groups
$b = (get-content $a\net_localgroup_list.txt) -notmatch '^\s*$' | select-object -skip 2 | select-object -skiplast 1

# removes asterisks at beginning of each new line
$c = foreach ($item in $b) { $item.SubString(1) } 
$c | out-file $a\net_localgroup_list_clean.txt

# outputs group members for each group into a single file separated by white space and divider
$output = foreach ($item in $c) { 
write-output "================================================================================"
write-output "$item Group Members"
write-output ""
# removes white space and non-relevant lines to just provide the members
net localgroup $item | select-object -skip 6 | select-object -skiplast 2
write-output "" 
}
$output | out-file $a\net_localgroup_list_members.txt

I am now trying to create something similar for older Windows machines and am running into a roadblock. Is there a way to edit things as much as possible in the command line without having to open up the file to edit it? The best I have been able to come up with so far is as follows; I am concerned that this would result in possibly missing some groups due to the limitations of findstr.

cd C:\Users\MyName\Desktop\
mkdir test
cd test
net localgroup > net_localgroup_list.txt

rem # removes white space, non-relevant lines, and asterisks to just provide the groups
findstr "*" net_localgroup_list.txt  > net_localgroup_list_clean.txt
(for /f "tokens=1,* delims='*'" %i in (net_localgroup_list_clean.txt) do @echo %i) > net_localgroup_list_clean2.txt

rem # Combining them into a single file
for /f "tokens=*" %i in (net_localgroup_list_clean2.txt) do net localgroup %i >> net_localgroup_list_clean3.txt
findstr /bvi "Member Comment ---- The" net_localgroup_list_clean3.txt | > net_localgroup_members.txt

I understand there is some functionality from the PowerShell commands that may not be able to be emulated here. However, I'd like to get as close as possible to it. Does anyone have any recommendations for making the Windows commands more efficient, such as not needing to create so many files or ensuring that groups are not accidentally omitted from the findstr options selected? At this time, I am not trying to create a batch file.

2
  • 1
    Older Windows? We talking Windows 10 old or something that is insecure unsupported and unable to have PowerShell 7 installed on it? Every version of Windows that you could be using except perhaps XP supports PowerShell 5.1 out of the box.
    – Ramhound
    Commented Jun 3 at 2:35
  • Yes. Some components on an isolated network have versions of Windows older than 10. Upgrades are planned, but its going to be a while before that happens. Commented Jun 3 at 2:40

1 Answer 1

1
@echo off && setLocal EnableDelayedExpansion

cd /d "%UserProfile%\Desktop"
2>nul mkdir .\test && cd /d "test"

set "_log_file=%UserProfile%\Desktop\test\net_localgroup_members.txt"
>"%_log_file%" cd.

for /f usebackq^tokens^=1*^delims^=* %%i in =;(` 
    =;( 
        net localgroup ^| findstr /bl * ^| findstr /bvi "Member Comment ---- The" ^| more +6 ^| sort /r ^| more +2 ^);= ^| sort /r 
    `);= do >>"%_log_file%" echo/%%~i

timeout -1 | type "%_log_file%" & endlocal

More can operate the skips on the initial lines, after that redirect to sort /Reverso and again skip the 2 (last) initial lines in the output, and at that point, sort /Reverso again to obtain the totalized output, skipping the 6 initials plus the last 2 lines.


I don't want to suddenly change your PowerShell script, but here are some suggestions that might be of interest to you

$old_ErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'

Set-Location $env:userprofile\Desktop
$a = "$env:userprofile\Desktop\pwd\net_localgroup_list.txt"

New-Item -Path $a -ItemType "file" -Force | Select-Object FullName 
New-Item -ItemType "directory" -Path "$env:userprofile\Desktop\pwd\test" | Select-Object FullName 
$ErrorActionPreference = $old_ErrorActionPreference 


(net localgroup | sls -SimpleMatch *) -notlike '*System Managed Accounts Group' | out-file $a
# removes white space and non-relevant lines to just provide the groups
$b = (get-content $a) -notmatch '^\s*$' | select-object -skip 2 | select-object -skiplast 1

# removes asterisks at beginning of each new line
$c = foreach ($item in $b) { $item.SubString(1) } 
$c | out-file $a.replace("_list","_clean")

# outputs group members for each group into a single file separated by white space and divider
$output = foreach ($item in $c) { 
      write-output "================================================================================"
      write-output "$item Group Members"
      write-output ""
      # removes white space and non-relevant lines to just provide the members
      net localgroup $item | select-object -skip 6 | select-object -skiplast 2
      write-output "" 
    }
$output

ps. At some point I may not have clearly understood some point in the question, forgive my language difficulties... so if something doesn't match the question, please point it out in another way...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .