How I can get the displayname and the office from a samaccountname list (.txt)? After that I want to save the displaynames and the offices to a .csv file. Here is a approach:
$users = Get-Content C:\TMP\test.txt
foreach ($user in $users)
{
Get-ADUser -ldapfilter "(samaccountname=$user)" -Property name, office | Select-Object -Property Name, Office
}
It should look like:
Hope you can help me?
You are asking for the Export-CSV command but from your comments you might be having issues with placement or your construct of foreach.
Lets try this then
$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation
Update from comments
Was having issues understanding your output from the comments which was why I wanted more that just a picture of the headers. I don't an issue with this code and the text should be quoted so special characters, like commas, should not be an issue. Please update you question with the text content of a sample file and your PowerShell version in case that is coming into play.
Use the delimiter parameter at the end
$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation -Delimiter ";"
Related
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
I've tried lots of different combinations at this point and I'm coming up dry. I have a CSV file that contains usernames (Users) of people in the format of 117321, which refers to their login name. I'm trying to get the homedirectory path of all these users and export them to a CSV. Here's what I have so far, but it doesn't seem to work. I've even tried filter.
$InputFile = 'C:\Users.csv'
$Users = Import-CSV $InputFile
$OutputFile = 'C:\Directory Results.csv'
$HomeDirOutput = ForEach ($User in $Users) {
Get-ADUser -LDAPFilter "(sAMAccountName=$user)" -Properties homedirectory
}
$HomeDirOutput | Export-Csv $OutputFile -NoTypeInformation
All I'm getting is a blank spreadsheet.
sAMAccountName is a valid value to pass to the -Identity parameter of Get-ADUser. Assuming that the CSV contains a column "AccountName", you should be able to do
Import-CSV -Path $InputFile | ForEach-Object { Get-ADUser -Identity $_.AccountName -Property sAMAccountName,HomeDirectory } | SelectObject -Property sAMAccountName,HomeDirectory | Export-CSV -NoTypeInformation -Path $OutputFile -Append
(after making sure that the output file doesn't already exist).
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
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.
Can someone help me?
I have to create a .txt file with the following format:
user("SamAccountName","GivenName Surname"){}
I'm able to create just this:
#Get AD Users Info
cls
$SamAccountName = New-Item 'c:\SamAccountName.txt' -type file -Force
Get-ADUser -Filter * -Properties SamAccountName |
select -First 15 | Sort-Object SamAccountName |
Format-Table SamAccountName | Out-File $SamAccountName
$content = Get-Content $SamAccountName
$content | Foreach {$_.TrimEnd() } | where {$_ -ne ""} | Select-Object -Skip 3 | Set-Content $SamAccountName
#Write quotes (make it nice and readable!)
$ACTIVEDIRECTORY = New-Item 'c:\ACTIVEDIRECTORY.lst' -type file -Force
Clear-Content $ACTIVEDIRECTORY
$quotes= '"'
(Get-Content $SamAccountName) |
ForEach-Object {Add-Content $ACTIVEDIRECTORY "$quotes$_$quotes"}
Get-Content $ACTIVEDIRECTORY
This give me this result:
"GivenName"
"GivenName"
"GivenName"
I agree with #notjustme about your question being a mess, but I'll throw an answer out there anyway.
Your first problem is how you set these two variables. They end up being objects, but you're trying to use them like file paths.
$SamAccountName = "c:\SamAccountName.txt"
$ACTIVEDIRECTORY = "c:\ACTIVEDIRECTORY.lst"
This will get you a comma separated list of your users, which is (sort of) what part of your code is doing.
Get-ADUser -Filter * | Select-Object SamAccountName, GivenName, SurName -First 15 | Sort-Object SamAccountName | Export-Csv $SamAccountName -NoTypeInformation
This will get you the list of users with the string format you specified at the top, which is odd, but whatever.
Import-Csv $SamAccountName | ForEach-Object {"user(`"$($_.SamAccountName)`",`"$($_.GivenName) $($_.Surname)`"){}"} | Out-File $ACTIVEDIRECTORY
Can't try it out myself at the moment but... from experience...
-properties * | select SamAccountName, GivenName, Surname
Should get ya in the ballpark.
Re-read your code a few times and well... it seems a mess to be honest. You state what you wanna accomplish and then there's a crud-load of other stuff that make close to no sense in this case. Maybe I'm just too tired, I'll check back in the morning and edit/delete if needed.
Allright, re-read a few more times and the headache is given but still...
In general I'd like to advise you (or anyone, really) if Powershell get's confuzzling to break it down in easily processable pieces.
For the users;
$users = Get-ADUser -Filter { Name -like '*' } -Properties * | select SamAccountName, Givenname, Surname
For the output;
foreach ($user in $users)
{
'("{0}""{1}{2}"' -f $user.SamAccountName, $user.GivenName, $user.Surname
}
Unfortunatly I have no way to try this out myself at the moment but it should be in the ballpark... if you end up getting some errors, lemme know and I'm sure I can guide ya through it. I'm not 100% sure about them single and doublequotes in this particular case... I'd have to try it out.