As Figure 1 shows, the NetUsers output contains a lot of extraneous lines. You need only the lines with the user information—in this case, the lines that start with MyDom\ fredsmith and MyDom\donnawong. To filter the NetUsers output so that you get only the lines that contain the user information, you can use the Find command with the /V switch. This switch tells the Find command to display all lines that don't contain the specified text. So, in this example, you use the command
NetUsers.exe \\work1 /h
Find /V " -"|
Find /V "successfully."|
Find /V "History"|
Find /V "Connecting"
Figure 2 shows the output from this command. You now have only the lines you need (aka the filtered strings), but there's another problem. The filtered strings contain varying numbers of spaces, which creates problems when you try to embed the strings in a For command. So, you need to do some creative coding to retrieve the four parts—the user ID (i.e., Domain\UserID), the friendly name, and the date and time of last logon—of each filtered string.
Assuming that the user IDs don't contain any spaces, the first part of the filtered string will always be the Domain\UserID. Thus, you can use the For command to break the filtered string into pieces, or tokens, so you can retrieve the user ID. As callout E shows, GetUserProfiles.bat captures the user ID in the first token (%i variable) and assigns the rest of the filtered string to the Remainder variable.
You can't use tokens to retrieve the friendly name because the friendly name might include a middle initial or might consist of several elements separated by spaces (e.g., Le Pu, Pepe), which means the friendly name might be in two or more tokens. For example, if the user has a name with no middle initial (e.g., Smith, Fred), the date would be in the fourth token (%l variable). If the user has a middle initial (e.g., Wong, Donna J.), the date would be in the fifth token (%m variable).
Fortunately, you can perform some advanced string manipulations to get the friendly name and the date and time information. You're probably familiar with using the Set command to perform environment-variable string substitutions. For example, the command
Set %PATH:str1=str2%
expands the PATH environment variable, then replaces the string on the left side of the equal sign (str1) with the string on the right side of the equal sign (str2).
A less commonly used environment-variable string substitution lets you expand an environment variable and extract a specific number of characters from the end of the expanded string. For example, to expand the PATH environment variable and extract the last 5 characters from the end of the expanded string, you use the command
Set %PATH:~-5%
You can use this type of string substitution to extract the date and time information from the Remainder variable. (You can also use this type of string substitution to extract characters from the beginning of a string. For more information about this substitution, run the Set command with the /? switch.) As Figure 2 shows, the time is the last 5 characters in a filtered string and the date is characters 6 through 16 if you count backward from the end of a filtered string. Thus, you can use the code that callout F highlights to extract the date and time.
With the user ID and date and time information retrieved from the filtered string, all that remains is the retrieval of the friendly name. As callout G shows, you can use a series of string replacements to retrieve the friendly name.
Note that there is always more than one way to handle a task in a script. Another way to retrieve the friendly name would be to use environment-variable string substitution to skip over the last 16 characters in the Remainder variable's string, assign the resulting substring a variable, then remove any double spaces from the substring.