I'm using the following code to get a list of direct reports in Powershell:
import-module activedirectory
$identity = read-host "Enter username:"
Get-ADUser $identity -Properties directReports | select-object {$_.directReports} | export-csv "DirectReports-$identity.csv"
This outputs all of the direct reports to one row, such as "CN=User1,CN=User2,CN=User3".....
How can I modify this so it outputs each user to its own row?
I'm sure this isn't the cleanest solution:
Get-ADUser $identity -Properties directReports | select-object -expandproperty directReports|foreach-object{$_.split(",")[0].replace("CN=","")}| export-csv "DirectReports-$identity.csv"
You'll get each person's name (only) on its own line in the CSV file.
Get-ADUser $identity -Properties directReports |
Select-Object Name,#{n='DirectReports';e={$_.directReports.Split(',CN=',[System.StringSplitOptions]::RemoveEmptyEntries) -join ';'}} |
Export-Csv "DirectReports-$identity.csv"
Related
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
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
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}
I'm trying to use a list of usernames to perform a simple get-aduser command. It works fine for a single user, but I can't input a file to perform this for a list.
This command works fine for a single user:
get-aduser -identity myusername -properties passwordlastset, passwordneverexpires |
sort name | ft Name, passwordlastset, Passwordneverexpires | out-file c:\PS\Output.txt
This works fine, but rather than use -filter * for all AD or identity pointing to a file, I am completely lost. I have tried doing a get-content and link to a file but I'm just getting into a pickle.
If I have a text file with a list of usernames in, how do I run the above command against that single text file list, rather than all of AD?
As a side query, is there a way that I can perform the above command, but for a specific OU?
If you have a list that isn't an object, either import it to an object or iterate over the values
Try something like:
$Userlist = Get-Content -path 'c:\temp\test.txt'
$Results = $Userlist | ForEach-Object {
Get-aduser -identity $_ -properties passwordlastset, passwordneverexpires
}
$Results | sort name | ft Name, passwordlastset, Passwordneverexpires | out-file c:\PS\Output.txt
This will work as long as you supply valid SamAccountNames in your list
I would do it this way. You can pipe in identity byvalue. You can import the csv later and get objects back.
get-content userlist.txt |
Get-aduser -properties passwordlastset, passwordneverexpires |
sort name |
select Name, passwordlastset, Passwordneverexpires |
export-csv users.csv
# searchbase example
get-aduser -filter 'name -like "j*"' -SearchBase 'OU=People,DC=stackoverflow,DC=com'
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