To obtain the attribute from the CSV list - powershell

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}

Related

Checking ExtensionAttributes

I had a list of 50 users with the Active Directory extensionAttribute12 sent to me, I was told that extensionAttribute13 was mixed up. So far, as I go through and check in AD, I don't see this to be so.
I would like to use PowerShell to check the list he gave me and export to my own list without going one by one.
I have this and when I runs it seems to export all users, but in my list, I only have 1 user. Later, I would like to run on the list of 50. I don't understand why I get all of the users.
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Import-Csv C:\Users\rhyman\Documents\ExListTest.csv | ForEach {
Get-ADUser -Filter * -Properties UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName | `
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 | `
Export-CSV c:\allinfo.csv -NoTypeInformation
}
}
I've done some editing, this is what I have now:
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Identity "$UserList" -Properties UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName |
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 |
Export-CSV C:\Users\RHyman\Documents\allinfo.csv -NoTypeInformation
}
What I'm most concerned with is that the Get-Aduser has error:
Cannot find an object with identity: '#{UserPrincipalName=first.last#x.x.gov
It sees the name on the list but I may have to go to the csv file and change the way the list displays the names.
Revised script: Only thing now exported csv is empty
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Filter {(mail -eq "$UserList")} -Properties
UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName |
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 |
Export-CSV C:\Users\RHyman\Documents\allinfo.csv -NoTypeInformation
}
Finally got what I was looking for and it does what I wanted
This is what I needed and used Get-Content
Get-ADUser -Filter {mail -like $_}
Thanks for all the help!
You are passing entire csv [Get-ADUser -Identity "$UserList"] into Identity use below instead.
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Identity $UserList.UserPrincipalName

Use CSV file with Names to get ADUser Email Addresses

First, I would like to thank you for your help
This is my question
I've got a CSV file with one Line
DisplayName
John Doe
Jane Doe
And I would like to use this Names so I get their EmailAdresses from our AD
This is the code
Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $users) {
Get-ADUser -Filter "name -eq '$user.DisplayName'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties name, emailAddress | Select-Object Name, emailAddress
}
$List = Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8
But it is not working
Thanks a lot
As stated by #Abraham Zinala said, your code doesn't show you capturing your csv import into a variable. I'm assuming that's a typo/copy issue.
The issue I see in your code is you are referencing a property of the variable, which requires a subexpression
$userlist = Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $userlist) {
Get-ADUser -Filter "name -eq '$($user.DisplayName)'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties emailAddress | Select-Object Name, emailAddress
}
$List | Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8
You could also dereference the property as part of the foreach statement which would make the simple variable expand fine without subexpression.
$userlist = Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $userlist.Displayname) {
Get-ADUser -Filter "name -eq '$user'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties emailAddress | Select-Object Name, emailAddress
}
$List | Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8

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

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

Powershell Get-ADUser and variables

I am trying to write a Powershell script that will allow me to query the samaccountname and other fields from a list of e-mail addresses. (reverse lookup)
My email.csv file has a users header, followed by the list of e-mail addresses.
What am I doing wrong?
$users = Get-Content .\email.csv
$users | ForEach-Object {
Get-ADUser -LDAPFilter "(emailaddress=$users)" -Properties samaccountname | Select-Object -Property samaccountname
} | Export-Csv -Path .\emailexport.csv
you will need to change $_.emailaddress depending on your column title in your csv file. also, samaccountname is returned by default for get-aduser, so you can leave that part off.
$users = Get-Content .\email.csv
$users | ForEach-Object {
$email = $_.emailaddress
Get-ADUser -LDAPFilter "(emailaddress=$email)" -Properties emailaddress |
Select-Object -Property samaccountname, emailaddress
} | Export-Csv -Path .\emailexport.csv