0

I am trying to convert a PowerShell cmdlet's output from a table view, into something more like an array. I used a list format to cut as much junk data as possible. Currently, I use the following code to get this output:

Get-AccountsWithUserRight -Right SeNetworkLogonRight | Format-List -Property SID |Set-Variable -Name "Accounts"
$Accounts

Output:

SID : S-1-5-32-583

SID : S-1-5-32-551

SID : S-1-5-32-545

SID : S-1-5-32-544

I am unsure of how to use formatting/other properties to cut out the 'SID' string, and just return an output such as "S-1-5-32-583,S-1-5-32-551,etc". I need to do this as I wish to use the output in another cmdlet, and the extra data is causing errors.

Xioner
  • 3
  • 1

2 Answers2

1

Only ever use Format-* cmdlets for display formatting; never use them if data must be programmatically processed. Format-* cmdlets output formatting instructions, not data - see this answer.

Also, to save output in a variable, it's simpler to assign to it rather than to use Set-Variable:

# Save the account objects in a variable.
$Accounts = Get-AccountsWithUserRight -Right SeNetworkLogonRight

If you then want to output / use just the SID values:

$Accounts.SID
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I know this is an old thread and the original poster has moved on, but others might look at this, so here's a version that I think answers the original question all the way. My understanding is that he wants a variable to have all the SIDs in an array.

$Accounts = Get-AccountsWithUserRight -Right SeNetworkLogonRight | Select-Object -ExpandProperty SID

#OR

$Accounts = Get-AccountsWithUserRight -Right SeNetworkLogonRight | ForEach-Object { $_.SID }

Either one should give you what you wanted. The Accounts variable should now only have the SID without any other properties. I usually use the second example, but I've seen a lot of cases with the first one.