Hi All,
I am looking for a PowerShell script to Get SAMAccountName, Name and export the output to a CSV file.
I now we can get this using Get-ADUser but not sure. Please help.
Thanks in advance.
- Edited by Alfogeek 17 hours 55 minutes ago
Technology Tips and News
Hi All,
I am looking for a PowerShell script to Get SAMAccountName, Name and export the output to a CSV file.
I now we can get this using Get-ADUser but not sure. Please help.
Thanks in advance.
Please clarify what you are asking. It is very confusing and vague.
I am looking for a PowerShell script to Get SAMAccountName, Name and export the output to a CSV file.
Input file is users.csv which contain employeeIds
I know we can get this using Get-ADUser but not sure. Please help.
Thanks in advance.
To get a specific employee is
Get-AdUser -filter "employeeid -eq 'X12345'"
Import-Csv users.csv |%{Get-AdUser -filter "employeeid -eq '$($_.EmployeeId)'"} | Select Name, SamAccountName
Hi,
From a computer with ActiveDirectory Module for Powershell, run the following script :
import-Module activeDirectory $Users = Get-ADUser -Filter * | Select-Object -Property Name,SamAccountName; $USers | Export-Csv -Path ".\Users.csv" -Delimiter ';' -NoTypeInformation;
this produces a .csv file with information required.
Re
Hi,
From a computer with ActiveDirectory Module for Powershell, run the following script :
import-Module activeDirectory $Users = Get-ADUser -Filter * | Select-Object -Property Name,SamAccountName; $USers | Export-Csv -Path ".\Users.csv" -Delimiter ';' -NoTypeInformation;this produces a .csv file with information required.
Re
This one should be OK :
import-Module activeDirectory $Users = Get-ADUser -Filter * -Properties Name,SamAccountName,EmployeeID | ?{$_.EmployeeID -ne $null} $Users = $Users | Select-Object -Property Name,SamAccountName; $USers | Export-Csv -Path ".\Users.csv" -Delimiter ';' -NoTypeInformation;
Reg
This one should be OK :
import-Module activeDirectory $Users = Get-ADUser -Filter * | ?{$_.EmployeeID -ne $null} $Users = $Users | Select-Object -Property Name,SamAccountName; $USers | Export-Csv -Path ".\Users.csv" -Delimiter ';' -NoTypeInformation;Reg
Regis - go back and read the OP carefully then apply the second OP post to my answer:
Import-Csv users.csv |%{Get-AdUser -filter "employeeid -eq '$($_.EmployeeId)'"} | Select Name, SamAccountName
I only left off the obvious ending Export.
TO clarify for ou what is happening.
In PS AD provider we can use server side filter in retrieve the user by employeeID. THat was my first line example:
Get-AdUser -filter "employeeid -eq 'X12345'"
Now to get the items requestiod we can just select for them:
Get-AdUser -filter "employeeid -eq 'X12345'" | Select Name, SamAccountName
To get the users from a list we can import the CSV and pipe it too out AD call:
Import-Csv users.csv |%{Get-AdUser -filter "employeeid -eq '$($_.EmployeeId)'"} | Select Name, SamAccountName
Here is a cleaner view:
Import-Csv users.csv | ForEach-OObject{ Get-AdUser -filter "employeeid -eq '$($_.EmployeeId)'" } | Select Name, SamAccountNameI know all of this is somewhat confusing to new user but by carefully analyzing and trying each new piece of code you will come up to speed fairly quickly.
To clarify the reason for the server side filter.
If we just do: "Get-AdUser -filter * -properties * " we will return every user in AD and every property. This can be very impacting oon AD and the network. That is the reason for the filter:
Get-AdUser -filter <someattribute operator valueto match>
The filter is executed at the server and uses the index if the attribute is indexed. It is much faster.
Hi Jrv,
Thanks for your remarks.
The Filter Attribute is interresting but if there are many records to find, this version will perform one query by employeeId in Csv file which is also time and load consuming.
I propose to first perform a global query and load results in a collection and then find elements in that collection.
import-Module ActiveDirectory # Load all users in Collection $Users = get-aduser -Filter * -Properties Name,SamAccountName,EmployeeID; $SourceFile = import-csv -Path users.csv; $OutputCollection = @(); Foreach ($element in $SourceFile) { $OutputCollection = $Users | ?{$_.EmployeeID -eq $element.EmployeeID} } $OutputCollection | export-csv -Path .\output.csv -NotypeInformation
the output csv file will only contains information about found users.
Regards,
Ok that is slightly better but not necessary unless the CSV has hundreds of entries and it will be extremely slow if the server has 100,000 users. YOu are also writing about three times as many lines of code as necessary.
In PowerShell V3 and V4 we don't need to use Import-Module as it is automatic.
To do it you way we would do this for simplicity and speed:
$empids = import-csv -Path users.csv |%{$_.employeeid} get-aduser -Filter * -Properties EmployeeID | Where{$empids -contains $_EmployeeID} | Select Name,SamAccountName,EmployeeID | export-csv -Path .\output.csv -NotypeInformation
It will still be slower than my way if there are more than 500 users and it will tax the server and the network more than necessary.
Rgis Lain, your method is very taxing.
Let's say I have a CSV file with 10 users I want to query in a directory with 10.000 users.
You're first retrieving connecting to the DC, asking for 10.000 users, sending it over the wire, saving 10.000 users onto memory and then cycling through each of the 10.000 users to see which has an EmployeeId I asked for.
Would it not be better to tell the DC exactly what you're after (which is user with EmployeeId = xxx), let the DC perform the query and get back to you with the results, which in this case will be just 3 users?
jrv's answer is the most concise and easier to understand, but if we're talking purely about performance it can be improved, specially because EmployeeId is not an indexed property, so performing x number of queries where x is the number of lines in the csv instead of just the one query is more taxing (it means the DC has to perform x amount of searches instead of just one search for all users).
$LdapFilter = '(|' import-csv -Path users.csv | ForEach-Object { $LdapFilter += "(employeeId=$($_.EmployeeId))" } $LdapFilter += ')' Get-AdUser -LdapFilter $ldapFilter -Properties name, sAMAccountName, employeeId | Select-Object name, sAMAccountName, employeeId | Export-Csv -Path .\Output.csv -NoTypeInformation@jrv, this is just to show the impact that bringing more than required can potentially have, not arguing with your solution at all, and yours is the best answer.