Get Samaccountname from display name into .csv - powershell

I have a .csv file with displayName and then the display names of the users.
I am running the following code, but the returned .csv file is blank 0KB. I've spent hours on this and I can't figure out what I'm doing wrong.
(I've even tried switching the displayName to "DisplayName" but that doesn't work)
Get-Content C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$user'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
I just need a simple return of the displayName = samaccountname

If your file is in fact a Csv file with a header for each column,
and displayname is one of them - then use Import-Csv to get the data.
ForEach-Object uses the variable $_ or alternatively $PSItem to assign the currently iterated row of data with the columns as properties.
So change to:
Import-Csv C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
Read this helpful answer on the issue of Get-AdUserand -filter from mklement0

Alright guys, I figured out the problem. The reason the input file was only returning some names is because the some of the names had spaces. For example
Doe, John was written as
Doe , John
with an extra space between the last letter of the last name and comma. To get just the displayName I used the following script:
Import-Csv C:\Scripts\inputfile.txt | ForEach {
Get-ADUser -Filter "displayName -eq '$($_.displayName)'" -Properties Name, SamAccountName |
Select Name,SamAccountName
} | Export-CSV -path C:\output\outputfile.csv -NoTypeInformation

Related

List properties of users using foreach comment

My goal is to list extended properties of a list of users by Display Name or SamAccountName pulling those names from a Csv. I am using the following script and it works but it either skips names in the Csv or repeats them. If I do one at a time it returns what I need but from the Csv it doesn’t. Csv has one column named Name.
Import-Csv C:\Users\Administrator\Documents\test.txt | Foreach {
Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" -Properties *
} | Select-Object DisplayName, SamAccountName, Title, Department, EmailAddress, ObjectGUID | Sort-Object Displayname | FT
There is nothing wrong with your current code, except for using Import-Csv on a .txt file (test.txt), I would assume this was a typo. I've added an if condition to help you troubleshoot so at least you would know which users where not found.
You should also avoid the use of -Properties *, querying all properties for the users is inefficient and slow.
$properties = #(
'DisplayName'
'SamAccountName'
'Title'
'Department'
'EmailAddress'
'ObjectGUID'
)
Import-Csv C:\Users\Administrator\Documents\test.csv | ForEach-Object {
$adUser = Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" -Properties $properties
if(-not $adUser) {
Write-Warning "'$($_.Name)' could not be found on AD"
return # Go next
}
$adUser
} | Select-Object $properties | Sort-Object Displayname | Format-Table

How would I get sAMAccountName and other user attributes from an input file of email addresses?

For the post answered Oct 31 '17 at 13:06:
Q:
I have a .txt file with a bunch of email addresses and I need get corresponding login names and export them into another file.
A:
Get-Content -Path "c:\users-input.txt" | %{ Get-ADUser -Filter {EmailAddress -eq $_} } | Select-Object SamAccountName | Export-CSV -Path "c:\users.csv"
Excellent, concise, single like example. Thanks!
Can you provide an example for outputing multiple attributes of the user accounts (ex: sAMAccountName, userPrincipalName, displayName, mail, userAccountControl) based on the input file being a list of email addresses?
Add the desired property names to the -Properties retrieved by Get-ADUser (SAMAccountName and UserPrincipalName will already be part of the default property set) and then specify the full property list selected by Select-Object:
Get-Content -Path "c:\users-input.txt" | %{
Get-ADUser -Filter {EmailAddress -eq $_} -Properties displayName,mail,userAccountControl
} | Select-Object SamAccountName,userPrincipalName,displayName,mail,userAccountControl | Export-CSV -Path "c:\users.csv"

collect samaccount powershell

I have a list of users in a CSV, but I need to collect the SamAccount attribute from each user by name in the ad.
CSV model
Script
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
I'm a little lost I don't know how to do a foreach using name
I am trying but without success.
Trying to get samaccountname based on Name on csv file.
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
and export to csv file.
Why use Get-ADObject and not Get-ADUser for this? The latter gives you more of the desired properties you need in the CSV.
As aside, it is wasteful to do -Properties * if all you want is a small set of user attributes.
Something like this should work:
Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
As per your comment you need to get some extra attributes of the users listed in the CSV, you can do this:
Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Hope that helps

To obtain the attribute from the CSV list

There is a list of users in CSV:
Name
surname name
I want to get to their posts. I use this script:
$userList = Import-Csv "C:\Scripts\names.csv"
foreach ($User in $userList) {
Get-ADUser -Identity $user.Name -Properties DisplayName, title |
select DisplayName, title |
Export-Csv -Append "C:\Scripts\title.csv" -NoTypeInformation
}
But the script does not find users.
If you are trying to get the users by their Name or DisplayName you will have to go with Filter instead of Identity. The following should work, assuming your actual AD-users Names are in the format of the csv.
$userList = import-csv "C:\Scripts\names.csv"
ForEach($User in $userList){
Get-ADUser -Filter "Name -eq '$($user.Name)'" -Properties DisplayName,title |
select DisplayName,title | Export-CSV -Append "C:\Scripts\title.csv" -NoTypeInformation}

PowerShell Import-CSV "filter out blank rows" Export-CSV

I am exporting all AD user accounts, formatting into a CSV file sorted in the way management wants it. They want it sorted by the Department field and remove all accounts without a Department code listed.
I have cobbled together a script to get a formatted CSV file:
Import-Module ActiveDirectory
Get-ADUser -filter * -properties Name, Title, Department, SamAccountName |
Select-Object GivenName, Surname, Title, Department |
Export-CSV -Path "\\Server\Share\file.csv" -NoTypeInformation
'Filter only users with DEPARTMENT populated'
$BlankColumns = "Department"
Import-CSV \\JXWFILEPRD01\Les$\file.csv |
Where-Object {
$line = $_
($BlankColumns | ForEach-Object{
![string]::IsNullOrEmpty(($line.$_.Trim('"')))
}) -notcontains $false
} | Export-CSV -Path "\\Server\Share\out.csv"
However, when I import the CSV and remove the rows we don't want, it spills the output to the console. I have not figured out how to use Export-CSV in a way that works. It either errors out or continues to dump to console and then error out.
You can do without importing/exporting multiple times and filter at the start:
Import-Module -Name ActiveDirectory
Get-ADUser -Filter 'Department -like "*"' -Properties Name,Title,Department,SamAccountName |
Select-Object -Property GivenName,Surname,Title,Department |
Sort-Object -Property Department |
Export-CSV -Path '\\Server\Share\file.csv' -NoTypeInformation
try this:
Import-Module ActiveDirectory
Get-ADUser -filter * -Properties Name,Title,Department,SamAccountName | where {![string]::IsNullOrWhiteSpace( $_.Department)} |
Select GivenName, Surname, Title, Department |
Export-CSV "\\Server\Share\file.csv" -NoType