collect samaccount powershell - 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

Related

Get-ADGroup members email address attribute

I have this code that pulls members of all distribution groups. I'm trying to get the member's email address to the same csv file as a new column instead of overwriting the DL data.
Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' -Properties * | Select-Object name, mail, #{l='ManagedBy';e={$_.managedby -replace '^CN=|,.*$'}}, #{l='Members';e={$_.members -replace '^CN=|,.*$' -join ","}}, #{l='MemberOf';e={$_.memberOf -replace '^CN=|,.*$' -join ","}} | Sort-Object Name, mail, ManagedBy, Members, MemberOf | export-csv DL.csv
import-csv -Path DL.csv | ForEach-Object {
Get-ADUser -Filter {Members -like $_.members} -properties mail | Select-Object SamAccountName,Name,GivenName,mail
} | Export-csv -Path DL.csv -NoTypeInformation

Convert one item to lowercase before AD export

In an ActiveDirectory Export I want the mail adresses in all lowercase, I know about ToLower() but I'm struggling putting it in the right place:
(Powershell)
Get-ADUser
-SearchBase "OU=11-something,DC=somethingelse,DC=somethingelser"
-Filter {somefilters} -Properties name,mail
|Select-Object Name,(mail).ToLower()
| Export-Csv -Path "D:\Path"
-Encoding UTF8 -NoTypeInformation
Is it even possible with Get-ADUser ?
You can customize the Select-Object output using a calculated property:
Get-ADUser -SearchBase "OU=11-something,DC=somethingelse,DC=somethingelser"
-Filter {somefilters} -Properties name,mail
|Select-Object Name,#{N="Email";E={$_.mail.ToLower()}}
| Export-Csv -Path "D:\Path"
-Encoding UTF8 -NoTypeInformation
See: Select-Object Documentation

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

How to format multiple entries in Powershell

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"