export users

Glenn Maxwell 12,871 Reputation points
2023-01-27T15:33:18.2633333+00:00

Hi All

i have samaccount names in the below format in the csv file. some of the users are disabled. i want to import this csv file and pull only enabled samaccount account users . i want the output with samaccountname,displayname,UserPrincipalName. please guide me.

csv

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-01-27T20:07:03.88+00:00

    This works for me:

    import-csv C:\junk\input.csv |
        ForEach-Object{
            Get-ADUser -Filter "SamAccountName -eq '$($_.sname)' -and enabled -eq 'true'" | 
                Select-Object samaccountname, displayname, UserPrincipalName 
        } | Export-Csv C:\junk\output.csv -NoTypeinformation -Append
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-01-27T16:06:33.3233333+00:00

    Use Import-CSV and pipe the date into a ForEach-Object loop. Use a filter on Get-ADUser. Pipe the output of that into a Select-Object. Pipe the output from the ForEach-Object loop into Export-CSV.

    Here's an example of using the filter:

    Get-ADUser -Filter "samaccountname -eq '$($_.sname)' -and enabled -eq 'true'"
    
    0 comments No comments

  2. Glenn Maxwell 12,871 Reputation points
    2023-01-27T17:03:00.8633333+00:00

    i am getting error using the below syntax

    import-csv C:\temp\input.csv | Get-ADUser -Filter "samaccountname -eq '$($_.sname)' -and enabled -eq 'true'" | Select-Object samaccountname, displayname, UserPrincipalName | Export-Csv C:\temp\output.csv -NoTypeinformation -Append

    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.