Richmatheisen,
Script is working as expected. I appreciate your help.
Thanks
uday
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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-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-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
Richmatheisen,
Script is working as expected. I appreciate your help.
Thanks
uday
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--
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"
}
}
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
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