Mrcavey
Mrcavey

Reputation: 1

Getting user name through AD using powershell

I've been banging my head against this for a while and can't figure out why this isn't working.

Import-Module ActiveDirectory
$Users = Get-ADGroupMember -identity “Accounting” -recursive | select name | export-csv -path "D:\group.csv"
$csv = Import-Csv "D:\group.csv"
foreach ($user in $csv) {Get-ADUser -filter "'Name -eq  ""$user""'"}

Upvotes: 0

Views: 225

Answers (2)

Matt
Matt

Reputation: 46730

You have a couple of issues here I think.

  1. You export the data just to import it again. Do you even need a second copy of the data?
  2. Import-Csv will return an object array not just the names. There is more than one way to address this but this should be more what you are looking for

Import-Module ActiveDirectory
$Users = Get-ADGroupMember -identity “Accounting” -recursive | select name
foreach ($user in $Users) {Get-ADUser -filter {Name -eq $user.Name}}

Even that is more than it needs to be. Its redundant since Get-ADGroupMember already returns similar objects you need. What do you need to do with this data? If you really need Get-AdUser then just pipe the output to it.

Get-ADGroupMember -identity “Accounting” -recursive | Get-Aduser

Upvotes: 1

alroc
alroc

Reputation: 28194

$user is a row in the CSV file. You need to address the Name field of that row (you also have way too many single- and double-quotes).

foreach ($user in $csv) {Get-ADUser -filter "Name -eq  '$($user.name)'"}

Or you could make the CSV file just a vanilla listing of names, and not read it as CSV:

Get-ADGroupMember -identity “Accounting” -recursive | select -expandproperty name | out-file "c:\group.txt"

$csv = get-content -path "c:\group.txt"
foreach ($user in $csv) {Get-ADUser -filter "Name -eq  '$user'"}

But if you're only using the file as a conduit between the group search and the user lookup, you can skip that entirely by piping the output of Get-ADGroupMember directly to Get-ADUser.

Get-ADGroupMember -identity “Accounting” -recursive | Get-ADUser;

Upvotes: 0

Related Questions