'Exporting a Powershell object containing multiple sets to CSV
I have a Powershell object which is the result of DSInternals Test-PasswordQuality cmdlet that I'd like to use as a Power BI dataset; it's a bunch of sets of user accounts, ex.
PS C:\> $result | get-member
TypeName: DSInternals.PowerShell.PasswordQualityTestResult
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AESKeysMissing Property System.Collections.Generic.ISet[string] AESKeysMissing {get;set;}
ClearTextPassword Property System.Collections.Generic.ISet[string] ClearTextPassword {get;set;}
DefaultComputerPassword Property System.Collections.Generic.ISet[string] DefaultComputerPassword {get;set;}
DelegatableAdmins Property System.Collections.Generic.ISet[string] DelegatableAdmins {get;set;}
DESEncryptionOnly Property System.Collections.Generic.ISet[string] DESEncryptionOnly {get;set;}
DuplicatePasswordGroups Property System.Collections.Generic.IEnumerable[System.Collections.Generic.ISet[string]] DuplicatePasswordGroup…
EmptyPassword Property System.Collections.Generic.ISet[string] EmptyPassword {get;set;}
Kerberoastable Property System.Collections.Generic.ISet[string] Kerberoastable {get;set;}
LMHash Property System.Collections.Generic.ISet[string] LMHash {get;set;}
PasswordNeverExpires Property System.Collections.Generic.ISet[string] PasswordNeverExpires {get;set;}
PasswordNotRequired Property System.Collections.Generic.ISet[string] PasswordNotRequired {get;set;}
PreAuthNotRequired Property System.Collections.Generic.ISet[string] PreAuthNotRequired {get;set;}
SmartCardUsersWithPassword Property System.Collections.Generic.ISet[string] SmartCardUsersWithPassword {get;set;}
WeakPassword Property System.Collections.Generic.ISet[string] WeakPassword {get;set;}
PS C:\> $result | select LMHash
LMHash
------
{a_user, another_user, yet_another_user…}
I'd like to export this to one csv where each array is its own column. If I simply do
$result | export-csv .\result.csv -NoTypeInformation
I do get a separate column for each set, but then just the the Set object type under each, ex.
How can I get a list of users, with each user in it's own cell, like this?
UPDATE
As an example, one of the elements of $result.PSObject.Properties is:
MemberType : Property
Value : {user, another_user, another_user…}
IsSettable : True
IsGettable : True
TypeNameOfValue : System.Collections.Generic.ISet`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e]]
Name : LMHash
IsInstance : True
So I think I need to iterate through the $result.PSObject.Properties for the column names and then also iterate through essentially $(result.PSObject.Properties).Value to get the users into separate cells in the csv. But doing them via index, i.e. $(result.PSObject.Properties).Value[1] results in the property types in the csv instead of the actual values. I tried nested foreach loops but still couldn't quite crack it.
Solution 1:[1]
Given the scenario, you can use a loop (in this case I will use a for loop to demonstrate this) to iterate through the listings using a PSCustomObject:
$maxCount = [math]::Max($result.LMHash.Count,$result.ClearTextPassword.Count)
for ($i = 0; $i -lt $maxCount; $i++)
{
[PSCustomObject]@{
LMHash = $result.LMHash[$i]
ClearTextPassword = $result.ClearTextPassword[$i]
} #| Export-Csv -Path ".\result.csv" -Append -NoTypeInformation
}
$maxCount will be the max number of iterations the for loop should iterate to; given those two properties of LMHash, and ClearTextPassword.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


