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.