Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 9837

Trying to capture same functionality in Windows cmd prompts as in a PowerShell list of commands using net localgroup

$
0
0

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 testcd test$a = pwdnet 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 membersnet localgroup $item | select-object -skip 6 | select-object -skiplast 2write-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 testcd testnet localgroup > net_localgroup_list.txtrem # removes white space, non-relevant lines, and asterisks to just provide the groupsfindstr "*" 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.txtrem # Combining them into a single filefor /f "tokens=*" %i in (net_localgroup_list_clean2.txt) do net localgroup %i >> net_localgroup_list_clean3.txtfindstr /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.


Viewing all articles
Browse latest Browse all 9837

Trending Articles