Hey Grant,
What makes your approach interesting is the use of the predefined hash table variable $WIPMemberOf to use to calculate the value in the select-object. I had just never seen that approach before. With that said; however, this approach does end up breaking your information into multiple objects of just name and value as opposed to one object that can use the rest of the powershell cmdlets to control the presentation. Let me give an example of what I mean.
You can use Select object from your Get-ADUser results to just get the properties you want and create custom properties if needed.
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
@{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
@{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}}
Results:
SamAccountName MemberOf DirectReports
-------------- -------- -------------
user1 WindowsAdmins,CA Power Users export,Power 2. User,Power 1. User
Since this keeps all of the data in a single object, you can use the formats to change how the data is displayed
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
@{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
@{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}} |
Format-List
Results:
SamAccountName : user1
MemberOf : WindowsAdmins,CA Power Users
DirectReports : export,Power 2. User,Power 1. User
You can even export this using the output or export cmdlets like Export-CSV or Export-CliXML
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
@{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
@{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}} |
Export-Csv c:\temp\export.csv
As an additional option, you can also do a custom powershell object rather than using select-object if it make more sense, and gives you the same ability to pipe into other powershell cmdlets for additional processing.
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
ForEach-Object {
[pscustomobject]@{
SamAccountName = $_.samaccountname
MemberOf = ($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","
DirectReports = ($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","
}
}