Sumeet Singh
Sumeet Singh

Reputation: 121

Best Practice - Format Numbered List Output - PowerShell

Use Case

The following code lists the current User Accounts with SID values on the localhost

Outputting the format to a output numbered list will not separate values in a table

What is best practice to code a readable formatted output

Code

$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().groups
$accounts = $sid.Translate([System.Security.Principal.NTAccount]).value

$hash = @{"sid"=$sid.value;"Accounts"=$accounts}

$obj = New-Object -TypeName psobject -Property $hash

for ($i=0; $i -lt $obj.sid.Length; $i++) 
{"{0:D2}. {1}  -   {2}" -f ($i),$obj.Accounts[$i],$obj.sid[$i]}

Output

00. Everyone  -   S-1-1-0
01. NT AUTHORITY\Local account and member of Administrators group  -   S-1-5-114
02. BUILTIN\Administrators  -   S-1-5-32-544
03. BUILTIN\Remote Desktop Users  -   S-1-5-32-555
04. BUILTIN\Remote Management Users  -   S-1-5-32-580
05. BUILTIN\Performance Log Users  -   S-1-5-32-559
06. BUILTIN\Users  -   S-1-5-32-545
07. NT AUTHORITY\INTERACTIVE  -   S-1-5-4
08. CONSOLE LOGON  -   S-1-2-1
09. NT AUTHORITY\Authenticated Users  -   S-1-5-11
10. NT AUTHORITY\This Organization  -   S-1-5-15
11. MicrosoftAccount\[email protected]  -   S-1-11-96-3623454863-58364-18864-2661722203-1597581903-1914428568-11352006
56-1295981414-3729554605-567089576
12. NT AUTHORITY\Local account  -   S-1-5-113
13. LOCAL  -   S-1-2-0
14. NT AUTHORITY\Cloud Account Authentication  -   S-1-5-64-36

Upvotes: 2

Views: 404

Answers (2)

EBGreen
EBGreen

Reputation: 37740

There is nothing at all wrong with Martin's answer. However, if the goal is to allow someone running the script to select a user, then I am a fan of Out-Gridview (requires a GUI):

$list = for ($i=0; $i -lt $obj.sid.Length; $i++) 
{
    [PsCustomObject]@{
        SID = $obj.sid[$i]
        Accounts = $obj.Accounts[$i]
    }   
}
$selected = $list | Out-Gridview -PassThru
Write-Host ('You selected {0} - {1}' -f $selected.Accounts, $selected.SID)

Upvotes: 5

Martin Brandl
Martin Brandl

Reputation: 58961

I would create a list of objects with the two properties (SID and Account):

$list = for ($i=0; $i -lt $obj.sid.Length; $i++) 
{
    [PsCustomObject]@{
        SID = $obj.sid[$i]
        Accounts = $obj.Accounts[$i]
    }   
}

Then you get a nice output by just otuput the $list:

SID                                           Accounts                        
---                                           --------                                
S-1-1-0                                       Everyone                        
S-1-5-32-544                                  BUILTIN\Administrators          
S-1-5-32-559                                  BUILTIN\Performance Log Users   
S-1-5-32-545                                  BUILTIN\Users                   
S-1-5-4                                       NT AUTHORITY\INTERACTIVE        
S-1-2-1                                       CONSOLE LOGON                   
S-1-5-11                                      NT AUTHORITY\Authenticated Users
S-1-5-15                                      NT AUTHORITY\This Organization  
S-1-2-0                                       LOCAL                           

Upvotes: 4

Related Questions