How to parse Collection<PSObject> when it is in tabular format by using Format-Table command

721 views Asked by At

I am using PowerShell cmdlet viz. Get-SPUser through C# program as follows

**Get-SPUSer** -web <siteCollection> | **format-table** Name,LoginName....

After I invoke this command it gives me ouptut in form of Collection< PSObject >

Now I want to traverse this collection of PSObject to get the actual records i.e. users with values of properties mentioned in command.

I want to use format-table instead of select-object. How to parse collection of PSObject to fetch property=value of user using C# ?

2

There are 2 answers

0
BND On BEST ANSWER

There is no other way but to use Out-String cmdlet using pipe which provides output in flat string format. Command is as follows

Get-SPUSer -web SiteCollection | format-list -property prop1, prop2 .. | Out-String

After removing newline/carriage returns and splitting techniques with string, I am able to get expected output. This is very crud way of achieving it but I have no choice. If select-object command has provided good performance then I would have go for it for sure.

0
Ron Thompson On

Not really an answer to your question, but I've personally not enjoyed trying to make Powershell play well with others. If I were in your shoes, I'd start with this: https://social.msdn.microsoft.com/Forums/sharepoint/en-US/6e9b3606-4ee7-4253-bafc-0058d9fe2a71/how-to-get-spuser-from-display-name-with-c-code and work with C# entirely or Powershell entirely and see what I could get done there.