Cannot get information from Get-ADUser to output to CSV - powershell

I have a SUPER simple query of Get-ADUser select mail that I need to output to CSV. The query fetches the information that I am looking for and prints to screen but when I attempt to use the Export-Csv OR Out-File cmdlets it creates a blank document.
$Users = Import-Csv C:\users\bob\Desktop\Administrator.csv
foreach ($User in $Users) {
$User = $User.UserName
Get-ADUser $User -Properties * |
Select mail |
Export-Csv -Path C:\Users\miker99a\Desktop\DiscoveryED.csv
}

Im a chucklehead. The answer is -append, I was overwriting the entries as the script ran
$Users = Import-Csv C:\users\bob\Desktop\Administrator.csv
foreach ($User in $Users) {
$User = $User.UserName
Get-ADUser $User -Properties * | Select mail | Export-Csv -Path C:\Users\miker99a\Desktop\DiscoveryED.csv -Append
}

Related

Powershell get aduser query by the emailaddress

I'm trying to get the AD user list query by email address. I managed to get the report however, I can't export it to a CSV file. I don't know which part gets wrong.
really much appreciate it if someone can guide me.
Thanks.
here is the script I made.
$ADUsers = Import-csv "C:\temp\DevicesWithInventory_6db9330a-4377-4057-bc86-837f55fee3f6.csv"
$email= $user.Primaryuseremailaddress
foreach ($user in $ADUsers) {
get-aduser -Filter {emailaddress -eq $email} -Properties * | select name,mail, whencreated, company,cn,country
} | Export-Csv -path c:\temp\intune_add_country.csv -NoTypeInformation
this is the amended script
$ADUsers = Import-csv "C:\temp\DevicesWithInventory_6db9330a-4377-4057-bc86-837f55fee3f6.csv"
$result = ForEach ($user in $ADUsers) {
$email= $user.PrimaryuserUPN
get-aduser -Filter {emailaddress -eq $email} -Properties * | select name,mail, whencreated, company,cn,country
}
$result| Export-Csv -path c:\temp\intune_add_country.csv -NoTypeInformation
despite the error, I get the report result that I want.

use samaccountnames to export user properties(mainly just first name and lastname) to csv file

I have a CSV file containing the samaccount name of some users.
From this list, I want to export the properties of these users to a CSV file.
Kindly share the simplest possible way to do so in Windows Powershell ISE.
I have tried this :
Import-ModuleActiveDirectory
Import-CSV C:\scripts\list.csv | ForEach{Get-ADUser -Identity $samaccountname-Filter*-Properties*|export-csv c:\ADusers.csv
}
Thank you!
You didn't show us the first couple of lines of the CSV file.
A proper CSV file has multiple fields and a header line like this:
"AccountName","EmailAddress"
"doe","john.doe#example.com"
"kent","clark.kent#example.com"
If this is the case, do:
Import-ModuleActiveDirectory
$userProperties = 'GivenName', 'SurName', 'Initials'
Import-Csv -Path "C:\Scripts\List.csv" | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.AccountName)'" -Properties $userProperties -ErrorAction SilentlyContinue
if ($user) {
$user | Select-Object -Property $userProperties
}
} | Export-Csv "C:\ADUsers.csv"
If the file you load only has SamAccountNames each listed on a new line, then this is not a CSV file and you should use:
Import-ModuleActiveDirectory
$userProperties = 'GivenName', 'SurName', 'Initials'
Get-Content -Path "C:\Scripts\List.csv" | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties $userProperties -ErrorAction SilentlyContinue
if ($user) {
$user | Select-Object -Property $userProperties
}
} | Export-Csv "C:\ADUsers.csv"
As you can see, I'm not using the -Identity parameter here, because in case a user with that SamAccountName is not found, an exception is thrown.
This way, output is only generated when the user actually exists.
Also, it is a bad idea to use -Properties * when you only want some of the properties returned.
Hope that helps
if you wanna do this in the ISE, you probably dont need/want to use oneliner for that.
I would suggest to import the CSV first, and then run foreach.
$list = Import-CSV -path $filePath
$result = New-Object System.Collections.ArrayList
foreach ($name in $list){
$adUser=Get-ADUser -Identity $name
$result += $adUser
}
From here, you can start thinking of error handling etc.
This will help you:
Import-ModuleActiveDirectory
Import-CSV -Path "C:\Scripts\List.csv" | Foreach {
Get-ADUser -Identity $_ -Filter * -Properties *
} | Export-CSV "C:\ADUsers.csv"
Your code was not working because $samaccountname was empty and blank not containing the username. So I replaced it with the automatic variable $_
Put each SamAccountName on its own line in the list file.
Example:
user1
user2
user3
Change list.csv to a text file (list.txt) and try this:
$username = Get-Content "C:\scripts\list.txt"
ForEach($user in $username){
Get-ADUser -Identity $user | Select GivenName,Surname,Initials | Export-CSV -Path "C:\ADUsers.csv"
}

Finding users in AD

I am trying to use a text file of users e-mail address to find the samaccountname names of the corresponding users.
Clear-Host
Import-Module ActiveDirectory
$File = "C:\T2\Users.csv"
$Users = Get-Content $File
ForEach ($User in $Users)
{ Get-ADUser -Filter * | Where { $_.EmailAddress -eq $User } | Select SamAccountName -ExpandProperty SamAccountName }
I am trying to understand what I am doing wrong
If your file is actually a .csv (comma-separated values), what you're doing is getting raw string content with Get-Content. There is a cmdlet, Import-Csv, that will take your CSV and turn it into a powershell object so you can iterate over it and access the headers as properties like you're trying to do in your code:
$users = Import-Csv -Path '/path/to/file.csv'
Then in your loop, you can clean up and speed up your code by not querying the entire AD tree for each user:
foreach ($user in $users)
{
Get-ADUser -Filter "EmailAddress -eq '$($user.EmailAddress)'" |
Select-Object -ExpandProperty SamAccountName
}
Super simplified version:
#requires -Module ActiveDirectory
foreach ($user in Import-Csv -Path '/path/to/file.csv')
{
(Get-ADUser -Filter "EmailAddress -eq '$($user.EmailAddress)'").SamAccountName
}

Powershell Import-CSV and Foreach-Object, excluding with the CSV header

I'm trying to get the SAMAccountNames of one domain and compare them with their equals from another domain.
To get all users of dc1 I use:
Get-ADUser -Filter * -SearchBase $SearchBase | Select-Object SamAccountName |
Export-Csv -path $exports -encoding "unicode" -notype
and then I import the csv again and try to compare them for any differences
$readthat = Import-CSV $exports -Header SamAccountName | ForEach-Object {
$user1 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes
$user2 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes -Server $dc2
$modified = #{}
$attributes | Where-Object { $user1.$_ -ne $user2.$_ } | ForEach-Object {
$modified[$_] = $user2.$_
}
}
All that works great, except that it's also trying to find the SamAccountName which of course genereates an error because the SamAccountName = SamAccountName doesn't exit.
Any hints on how to avoid this or do you guys have a more elegant solution?
the .csv looks like this:
"SamAccountName"
"foo"
"bar"
Don't use the -Header SamAccountName option on your import-csv should help immensely. The -Header option is for when the CSV file you are importing doesn't have a header. The Export-CSV cmdlet puts the header in there for you, so you don't have to.

powershell change AD user attribute

I need to search through all users in AD, find their attribute "mail" and in this attribute replace #hell.com to #heaven.com
I'm stuck at exporting and importing to csv...
Import-Module ActiveDirectory
get-aduser -Filter {samaccountname -like "gmaleev"} -properties * | Select-Object SamAccountName,mail | export-csv -NoTypeInformation d:\1.csv
$impfile = "d:\1.csv"
Import-CSV $impFile
foreach ($user in $users)
{
$sam = $user.samaccountname
$email = $user.mail
write-host "User $sam has mail $mail"
}
It doesn't work, why?
The current issue is that you are running a cmdlet to import the data but not saving it. Your ForEach cmdlet is not processing any data so you should have blank output except that of Import-CSV which should be outputing to console.
Also it seems redundant to export the data to import it again. I will presume that you have a reason for exporting it.
Import-Module ActiveDirectory
$impfile = "d:\1.csv"
$results = Get-AdUser -Filter {samaccountname -like "gmaleev"} -properties * | Select-Object SamAccountName,mail
$results | export-csv -NoTypeInformation d:\1.csv
$results | foreach ($user in $users){
$sam = $user.samaccountname
$email = $user.mail
write-host "User $sam has mail $mail"
}
This should address what you are showing. It will get the same data and save it to the variable $results. Export that data and then piping it to the ForEach to process
Your title suggets that you want to change user attributes. To do that you would need to use Set-Aduser. Depending on what your environment is this might not be the best way to manipulate email attributes.