powershell script to export AD info to csv file

uday god 1 Reputation point
2022-06-04T09:09:32.843+00:00

I have return a powershell script to get AD user properties and his list of group memberships and exporting the results to csv file. I am reading a list of users from text file. Below is the script

$users = ForEach ($user in $(Get-Content C:\temp\MA_usersB.txt)) {

Get-ADPrincipalGroupMembership $user | select name, groupcategory | Get-ADUser -Filter "samaccountname -eq '$user'" -Properties SurName, GivenName, SamAccountName, Manager |
Select SurName, GivenName,SamAccountName, Enabled, @{label="Manager";expression={(Get-ADUser $_.Manager -Properties DisplayName).DisplayName}}

}

$users |
Select-Object SurName, GivenName, SamAccountName, Enabled, Manager, name groupcategory |
Export-CSV -Path C:\uday\MA\listusers.csv -notype

but i am getting Piping error any help in fixing the issue much appreciated.

PS C:\>
PS C:\> .\listuser.ps1
Get-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\temp\listuser.ps1:4 char:69

  • Get-ADPrincipalGroupMembership $user | select name, groupcategory | Get-ADUser - ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (@{name=Domain U...egory=Security}:PSObject) [Get-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Get-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\uday\listuser.ps1:4 char:69

  • Get-ADPrincipalGroupMembership $user | select name, groupcategory | Get-ADUser - ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (@{name=Dir-PMO_...egory=Security}:PSObject) [Get-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Get-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\uday\listuser.ps1:4 char:69

  • Get-ADPrincipalGroupMembership $user | select name, groupcategory | Get-ADUser - ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (@{name=Dir-PM_M...egory=Security}:PSObject) [Get-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-06-04T15:01:42.61+00:00

    Try this instead (the code has NOT been tested!):

    Get-Content C:\temp\MA_usersB.txt |
        ForEach-Object{
            $user = $_
            $props = [ordered]@{
                        GroupName = ""
                        GroupCategory = ""
                        SurName = ""
                        GivenName = ""
                        SamAccountName = ""
                        Enabled = ""
                        Manager = ""
                    }
            $u = Get-ADUser -Filter "samaccountname -eq '$user'" -Properties SurName, GivenName, SamAccountName, Manager
            if ($u){
                $props.SurName = $u.SurName
                $props.GivenName = $u.GivenName
                $props.SamAccountName = $u.SamAccountName
                $props.Enabled = $u.Enabled
                $props.Manager = (Get-ADUser $u.Manager -Properties DisplayName).DisplayName
    
                Get-ADPrincipalGroupMembership $user |
                    ForEach-Object{
                        $props.GroupName = $_.name
                        $props.GroupCategory = $_.GroupCategory
                        [PSCustomObject]$props
                    }
            }
            else{
                Write-Host "Did not find user $user"
            }
        } | Export-Csv -Path C:\uday\MA\listusers.csv -notype
    
    0 comments No comments

  2. uday god 1 Reputation point
    2022-06-04T15:49:14.857+00:00

    Thanks for quick response, I tried to execute the script but getting below error, please advice

    Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
    At C:\xxx\new.ps1:19 char:43

    • $props.Manager = (Get-ADUser $u.Manager -Properties DisplayName).Di ...
    • ~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

  3. Rich Matheisen 47,901 Reputation points
    2022-06-05T01:55:03.053+00:00

    Mark one of my posts as an answer to your question. If you have any further changes, start a new thread. This one has gone beyond answering the question you originally asked.

    To create a separate CSV for each user:

    Get-Content C:\temp\MA_usersB.txt |
        ForEach-Object{
            $user = $_
            $props = [ordered]@{
                        GroupName = ""
                        GroupCategory = ""
                        SurName = ""
                        GivenName = ""
                        SamAccountName = ""
                        Enabled = ""
                        Manager = ""
                    }
            $u = Get-ADUser -Filter "samaccountname -eq '$user'" -Properties SurName, GivenName, SamAccountName, Manager
            if ($u){
                $props.SurName = $u.SurName
                $props.GivenName = $u.GivenName
                $props.SamAccountName = $u.SamAccountName
                $props.Enabled = $u.Enabled
                $props.Manager = (Get-ADUser $u.Manager -Properties DisplayName).DisplayName
                [array]$a = @()
                Get-ADPrincipalGroupMembership $user |
                    ForEach-Object{
                        $props.GroupName = $_.name
                        $props.GroupCategory = $_.GroupCategory
                        $a += [PSCustomObject]$props
                        $props.SurName = ""
                        $props.GivenName = ""
                        $props.SamAccountName = ""
                        $props.Enabled = ""
                        $props.Manager = ""
                    }
                $a | Export-Csv -Path C:\uday\MA\$user.csv -notype
            }
            else{
                Write-Host "Did not find user $user"
            }
        }
    
    0 comments No comments

  4. Limitless Technology 39,916 Reputation points
    2022-06-07T07:40:15.417+00:00

    Hello Udaygod,

    The main issue is that you are quoting a variable like a char set: -eq '$user' it should be without any quotes as:

    Get-ADUser -Filter "samaccountname -eq $user"


    --If the reply is helpful, please Upvote and Accept as answer--


  5. uday god 1 Reputation point
    2022-06-07T15:15:17.837+00:00

    Richmatheisen,

    Script is working as expected. I appreciate your help.

    Thanks
    uday

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.