Powershell Request - Grab Attribute for Each Object

Isaias Perez
Isaias Perez used Ask the Experts™
on
I have a CSV File or Text File for 100s of Users in my environment. I would like to develop a script that would take each UPN on that column and give me back the data in the Attribute "Description" of that object which is where we put the users Title. Can anyone help?

$Users = Get-Content C:\users_upn.txt >>>>Give me back the data in the attribute field " Description" for each user. Write that data to a text or csv file.

I have a list of users but want to add their Titles to that spreadsheet and that data is stored in the "Description" attribute field.

Description-Attribute.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Something like this?
Get-Content C:\users_upn.txt | ForEach-Object {
	If ($adUser = Get-ADUser -Filter "userPrincipalName -eq '$($_)'" -Property Description) {
		$adUser
	} Else {
		[PSCustomObject]@{UserPrincipalName=$_}
	}
} | Select-Object -Property UserPrincipalName, SamAccountName, Description, DistinguishedName |
	Export-Csv -NoTypeInformation -Path "C:\Temp\ADUsers.csv"

Open in new window

Isaias PerezIT Operations Manager

Author

Commented:
This seemed to work partially. In other words there were some fields generated but most were just blank. I have another project like this now where i have a bunch of user principle names and i have to generate the attribute Description for those users. So find the adusers and then give me their titles (which we usually use the description field for).
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Not an issue with the script, rather with the input file; any row in the output where SamAccountName and DistinguishedName are empty means that the UserPrincipalName could not be found.
Isaias PerezIT Operations Manager

Author

Commented:
Ok thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial