Get-ADUser -Filter

Rising Flight 3,731 Reputation points
2021-10-01T05:31:16.933+00:00

Hi All

i have a single csv file which contains samaccount names and upns. i mean some are samaccountnames and some ups in the same csv file. i am trying the below syntax but i am not getting the output. experts guide me.

$list = Import-Csv C:\temp\input.csv
ForEach($user in $list){    
$dn = $user.user
Get-ADUser -Filter $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co  |Export-Csv C:\temp\output.csv -NoType -Append }
Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,457 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,370 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,122 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 39,351 Reputation points
    2021-10-01T13:02:02.273+00:00

    Hello @Rising Flight

    I am not really clear what your goal is. You can get aduser object using its Security Account Manager (samaccountname), distinguished name, SID, or GUID. Using Get-ADUser -Identity, you can get all of the properties for a specific user using Properties. You can get active directory user filter by user principal name. Or you are trying to find samaccountname from displayname with powershell?

    Import-Csv C:\Data\Working\users.csv –Delimiter ";" | foreach {Get-ADUser –LDAPFilter "(ObjectClass=User)(anr=$($_.displayname))" –Properties displayname, SamAccountName} | select-Object displayname,SamAccountName | Export-Csv –Delimiter ";" C:\Data\Working\Users_DN_SAM.csv –NoTypeInformation

    ------------------

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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-10-01T15:23:16.707+00:00

    Try this:

    Import-Csv C:\temp\input.csv |
        ForEach-Object{
            $samaccountused = $false
            $upnused = $false
            $aduserfound = $false
            $userID = $_.user
            Try{
                $user = Get-ADUser -Identity $_.user -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co -ErrorAction Stop
                $samaccountused = $true
                $aduserfound = $true
            }
            Catch{
                # do nothing -- will try UPN next
            }
            If (-not $user){
                $user = $user = Get-ADUser -Filter "userPrincipalName -eq '($_.user')" -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co
                if ($user){
                    $upnused = $true
                    $aduserfound = $true
                }
            }
            if ($user){
                $user | Select-Object DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co, @{n='UserID';v=$_.user}, @{n='samAccountUsed';v=$samaccountused}, @{n='$UpnUsed';c=$upnused},@{n='UserFound';v=$aduserfound}
            }
            else{
                "" | -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co, @{n='UserID';v=$_.user}, @{n='samAccountUsed';v=$samaccountused}, @{n='$UpnUsed';c=$upnused},@{n='UserFound';v=$aduserfound}
            }
        } Export-Csv C:\temp\output.csv -NoType
    
    0 comments No comments