question

RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 asked RichMatheisen-8856 answered

Get-ADUser -Filter

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-serverwindows-server-powershellwindows-server-2019windows-server-2016
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @RisingFlight-7863

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--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.