Pivot in PowerShell

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Ochness
Posts: 6
Joined: Fri Feb 15, 2019 7:12 pm

Pivot in PowerShell

Post by Ochness »

I am trying to get a list of users with some user properties as well the ADPrincipalGroupMembership. I have the script working but it give me all the property info for each group the user is a member of. Is there a way in PowerShell to pivot (or something like that) and have all the information on one line? So like linear....

Here is the code I'm using:
  1. $aduser = foreach ($User in (Get-ADUser -Filter "name -like '$name*'" -Properties * | where { $_.Enabled -eq $true }))
  2.  
  3. {
  4.  
  5.     Get-ADPrincipalGroupMembership -Identity $User |
  6.  
  7.         Select-Object @{ Name = "User"; Expression = { $User.Name } },
  8.  
  9.                       @{ Name = "SamAccount"; Expression = { $User.SamAccountName } },
  10.  
  11.                       @{ Name = "When Created"; Expression = { $User.whenCreated } },
  12.  
  13.                       @{ Name = "When Changed"; Expression = { $User.whenChanged } },
  14.  
  15.                       @{ Name = "Last Logon";  Expression = { $User.lastLogonDate } },
  16.  
  17.                       @{ Name = "Member Of"; Expression = { $_.Name } }
  18.  
  19. }
  20.  
  21.  
  22.  
  23. $aduser | Sort User
The data returned looks like this:
User: Username
SamAccount: SamAccountName
When Created: 11/30/2015 1:41:58 AM
When Changed: 10/8/2020 12:08:34 PM
Last Logon: 10/2/2020 2:39:40 AM
Member Of: TechnologyGroup

User: Username
SamAccount: SamAccountName
When Created: 11/30/2015 1:41:58 AM
When Changed: 10/8/2020 12:08:34 PM
Last Logon: 10/2/2020 2:39:40 AM
Member Of: Domain Users

User: Username
SamAccount: SamAccountName
When Created: 11/30/2015 1:41:58 AM
When Changed: 10/8/2020 12:08:34 PM
Last Logon: 10/2/2020 2:39:40 AM
Member Of: VPN Access

And it keeps going depending on how many groups the user is in.

Any suggestions or assistance here would be great.

Thank you.

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Pivot in PowerShell

Post by jvierra »

You are not defining "$user".

I recommend starting by reading this excell4ent book on PowerShell. It is really impossible to address your question given the code you posted.

https://www.sapien.com/books_training/W ... werShell-4

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Pivot in PowerShell

Post by jvierra »

Sorry - I see the code is badly formatted and hard to read.
Here is a better method of setting this up.

Code: Select all

Get-ADUser -Filter "name -like '$name*' -and Enabled -eq $true" |
    ForEach-Object{
        $user = $_
        Get-ADPrincipalGroupMembership $user |
            ForEach-Object{
                [pscustomobject]@{
                    User = $user.Name
                    SamAccount = $user.SamAccountName
                    WhenCreated =$user.whenCreated
                    WhenChanged = $User.whenChanged
                    LastLogon = $user.lastLogonDate
                    MemberOf = $_.Name
                }
        }
    }

Ochness
Posts: 6
Joined: Fri Feb 15, 2019 7:12 pm

Re: Pivot in PowerShell

Post by Ochness »

@jvierra are you an employee of Sapien Technologies?

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Pivot in PowerShell

Post by jvierra »

Ochness wrote:
Thu Oct 08, 2020 3:19 pm
@jvierra are you an employee of Sapien Technologies?
No. This is a public forum. Sapien has forums for licensed customers for product specific questions.

Your question is in the correct forum as it is about how to code in PowerShell. I am a moderator here and I am a broadly experienced systems programmer. I have been using PowerShell since the first invitation to preview the product.

Do you need product help from a Sapien employee?

Locked