I have a powershell script that I want to output to the console the info from get-localgroupmember (for admins) and then (separately) the info for get-localgroupmember (for Users) after that. But when I run the two, the results get combined and the output doesn’t display for the first call and then responds to the second/last call with all the results in the same objectclass list.
How do I get it to execute one at a time separately?
$userName = "Student"
Write-Host "`nShowing local user`n"
Get-LocalUser -Name $userName;
Write-Host "`nShowing Users group`n";
Get-LocalGroupMember -Group "Users";
Write-Host "`nShowing Administrators group`n";
Get-LocalGroupMember -Group "Administrators";
Write-Host "`nShowing local users`n"
Get-LocalUser -Name $userName;
I tried to assign each to their own variables and then using write-host to output each of the variables but it still did the same thing.
This is what I tried, got the same output
Write-Host "`nShowing Users group`n";
$object = Get-LocalGroupMember -Group "Users";
Write-Output $object
Write-Host "`nShowing Administrators group`n";
$object = Get-LocalGroupMember -Group "Administrators";
Write-Output $object
if I run it all together I get this:
Showing local user
Showing Users group
Showing Administrators group
Name Enabled Description
---- ------- -----------
Student True
Name : MSI\user
SID : S-1-5-21-1457208496-2238809947-3401341024-1001
PrincipalSource : MicrosoftAccount
ObjectClass : User
Name : MSI\student
SID : S-1-5-21-1457208496-2238809947-3401341024-1004
PrincipalSource : Local
ObjectClass : User
Name : NT AUTHORITY\Authenticated Users
SID : S-1-5-11
PrincipalSource : Unknown
ObjectClass : Group
Name : NT AUTHORITY\INTERACTIVE
SID : S-1-5-4
PrincipalSource : Unknown
ObjectClass : Group
Name : MSI\Administrator
SID : S-1-5-21-1457208496-2238809947-3401341024-500
PrincipalSource : Local
ObjectClass : User
Name : MSI\user
SID : S-1-5-21-1457208496-2238809947-3401341024-1001
PrincipalSource : MicrosoftAccount
ObjectClass : User
If I remove the Get-LocalUser -Name $userName; call, I get this:
Showing Users group
Showing Administrators group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\user MicrosoftAccount
User MSI\student Local
Group NT AUTHORITY\Authenticated Users Unknown
Group NT AUTHORITY\INTERACTIVE Unknown
User MSI\Administrator Local
User MSI\user MicrosoftAccount
I want two separate objectclass lists with space in between. As if I typed each one in separately by hand, hitting enter after each command. See what I am trying to accomplish here:
Showing Users group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\user MicrosoftAccount
User MSI\Student Local
Group NT AUTHORITY\Authenticated Users Unknown
Group NT AUTHORITY\INTERACTIVE Unknown
Showing Administrators group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\Administrator Local
User MSI\user MicrosoftAccount
Showing local users
Name Enabled Description
---- ------- -----------
student True