Celeb Glow
updates | March 14, 2026

Write Output in table format in PowerShell

function Get-PCinfo { <# .Synopsis Short description .DESCRIPTION Long description .EXAMPLE Example of how to use this cmdlet .EXAMPLE Another example of how to use this cmdlet .NOTES <Zadanie 5> <Author email> #> [CmdletBinding()] Param( # Param1 help description [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$ComputerName = $env:COMPUTERNAME ## Param2 help description #[int] #[switch]$outFile = $false ) Begin{ $Info = @() $Info | Format-List } Process{ foreach ($computer in $ComputerName) { $NotReachableComputers=$null Write-Host "Testing connectivity $computer ..... please wait" -ForegroundColor White if (Test-Connection $computer -Quiet -Count 1) { Get-CimInstance win32_UserAccount | ForEach-Object { $PCInfo = [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME Name = [string](Get-CimInstance win32_UserAccount).Name SID = [string](Get-CimInstance win32_UserAccount).SID Lockout = [string](Get-CimInstance win32_UserAccount).Lockout Disabled = [string](Get-CimInstance win32_UserAccount).Disabled LocalAdminMember= $_.LocalAdminMember } } } else { $NotReachableComputers += $computer.name } } } End{ if($NotReachableComputers -ne $null){ Write-Host "This system is not available on network" -ForegroundColor Red Write-Host $NotReachableComputers } else { Write-Host "Code worked on all PCs" -ForegroundColor Green Write-Output $PCInfo } #List of systems that were not available on the network #List of output from systems that were available #List where the output file is located } } 

My problem is that the output is all together but I want it separately

So my output look like thisenter image description here

Or like this

enter image description here

What I want is to have a table like

ComputerName Name SID .....
PCname Name1 SID1 ......
PCname Name2 SID2.....
3

2 Answers

Your function never specifies -ComputerName $computer for your Get-CimInstance commands. This means it only ever runs on the local machine. Try and update it like so:

foreach ($computer in $ComputerName) { if (Test-Connection $computer -Quiet -Count 1) { Get-CimInstance win32_UserAccount -ComputerName $computer

The function does not specify which user's details you want within $PCInfo. You also do not need to query multiple times since your query is identical for each user's properties. Use Select-Object instead to include multiple properties. Aditionally, you can speed up the process by filtering for only the local accounts with -Filter "LocalAccount=True":

$PCInfo = foreach ($computer in $ComputerName) { Write-Host "Testing connectivity $computer ..... please wait" -ForegroundColor White if (Test-Connection $computer -Quiet -Count 1) { Get-CimInstance win32_UserAccount -ComputerName $computer -Filter "LocalAccount=True" | Select-Object Name,SID,Lockout,Disabled,LocalAdminMember }
}
Write-Output $PCInfo

And two more general points:

  • Win32_UserAccount objects do not contain a property with the name LocalAdminMember. You would need to find that information a different way.
  • Test-Connection $computer fails when $computer is the local machine

#1 PSSession would be better for this.

#2 Your problem is here:

Get-CimInstance win32_UserAccount | ForEach-Object { $PCInfo = [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME Name = [string](Get-CimInstance win32_UserAccount).Name SID = [string](Get-CimInstance win32_UserAccount).SID Lockout = [string](Get-CimInstance win32_UserAccount).Lockout Disabled = [string](Get-CimInstance win32_UserAccount).Disabled LocalAdminMember= $_.LocalAdminMember } }

Running this command like this just runs "Get-CimInstance win32_UserAccount" 4 different times. Each time the command is run it returns an array when you then pend .Name ,to the result i.e.(command).Name, selects the property "Name" from all the objects in the array, finally you are storing them against the key "Name" in the new PSCustomObject. that is why you are getting the second output above.

You should be running it like this:

$PCInfo will be initilized as an empty system.array

$PCInfo = @()

Get-CimInstance will be run one time then its results will be sent through the pipe | to the foreach-object this processes each object, selects the property for the individual object processed, then appends it to the PCInfo system array (+= does this for you)

Get-CimInstance win32_UserAccount | ForEach-Object { $PCInfo += [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME Name = $_.Name SID = $_.SID Lockout = $_.Lockout Disabled = $_.Disabled LocalAdminMember= $_.LocalAdminMember } } 

After this $PCInfo | Format-Table should give it to you in the manner you are looking for.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy