Get-ADUser from Array

Welcome Forums General PowerShell Q&A Get-ADUser from Array

This topic contains 2 replies, has 2 voices, and was last updated by

10 months ago.

  • Author
  • #99874

    Points: 0
    Rank: Member

    Hello all,
    Trying to get specific details of users in a group using Powershell. The code I'm using is:

    # Define variables
    $adGroupMembers = @()
    $adGroupMembersDetails = @()
    $adGroupName = read-host "Please Enter Group Name"
    #Get group members
    $adGroupMembers = Get-ADGroupMember $adGroupName | Select SamAccountName
    ForEach ($groupMember in $adGroupMembers) {
    	Get-ADUser $groupMember -Properties SamAccountName,Surname,GivenName,Mail,Enabled | Select SamAccountName,Surname,GivenName,Mail,Enabled | Export-CSV "C:\PowerShellScripts\Output\AD_User_Info.csv" -Append -notypeinformation

    The error I receive is:
    Get-ADUser : Cannot find an object with identity: 'samaccountname -eq '@{SamAccountName=SAMACCOUNTNAME}' under:
    At C:\PowerShellScripts\Scripts\GetGroupMembers.ps1:12 char:2
    + Get-ADUser $filterstring # -Properties SamAccountName,Surname,Giv ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (samaccountname ...tName=SAMACCOUNTNAME}:ADUser) [Get-ADUser], ADIdentityNotFo
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M

    The last part works fine if pulling from a text file or a .csv, but not if it's pulling from an array. I've read posts where Get-ADUser can't pull from an array, but once it's in a ForEach, it's referencing a single variable pulled from the array, not the entire array, correct?

    Thanks in advance for any help.

  • #99877

    Points: 403
    Helping Hand
    Rank: Contributor
    $adGroupMembers = Get-ADGroupMember $adGroupName | Select SamAccountName

    Here's your issue. So, by default, Select-Object keeps the properties in an object, even if you have only one. Add the -ExpandProperty (or just -Expand for short) parameter to have it pull the actual string out so you can use it directly:

    $adGroupMembers = Get-ADGroupMember $adGroupName | Select-Object -ExpandProperty SamAccountName
  • #99879

    Points: 0
    Rank: Member

    Worked like a charm! Thank you!

The topic ‘Get-ADUser from Array’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort