Asked by:
Help with foreach loop

Question
-
Hi Everyone,
Objective:
Get active directory properties of multiple users and save the info to a CSV file.
I have a text file containing the usernames (C:\temp\test\test1.txt)
The code is working, but for some reason it only read the last line of the text file (test1.txt) so only returns value for a single user each time its run.
Its not reading the text file properly and I cant figure out why. The code is below, please help:
$Users = get-content "C:\temp\test\test1.txt"
foreach ($User in $Users) {
Get-ADUser -identity $user -Properties displayName,SamAccountName,description,mail,company,manager,accountExpires,memberOf | % {
New-Object PSObject -Property @{
UserName = $_.DisplayName
UserId = $_.SamAccountName
description = $_.description
Mail = $_.mail
company = $_.company
manager = $_.manager
accountExpires = $_.accountExpires
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","
}
} | Select UserName,UserId,description,Mail,company,manager,accountExpires,groups | Export-Csv C:\temp\ADreport.csv -NTI
}
Tuesday, May 22, 2018 8:42 AM
All replies
-
You likely have a bad text file.
\_(ツ)_/
Tuesday, May 22, 2018 8:52 AM -
Thanks. I have tried recreating the text file but still no joy. I,ve also tries using CSV file for the username but that also does not work.
It seems to just ignore all the lines in my text file and only read the last line of my file. So I get the results exported correctly to ADreport.csv for one user only. But they are 300 in the text file.
Its driving me crazy :-(
Tuesday, May 22, 2018 9:02 AM -
Add "Append" to your export.
\_(ツ)_/
Tuesday, May 22, 2018 9:03 AM -
Unfortunately that results in errors (-append)
Export-Csv : A parameter cannot be found that matches parameter name 'Append'.
At line:13 char:129
+ } | Select UserName,UserId,description,Mail,company,manager,accountExpires,gr
oups | Export-Csv C:\temp\ADreport.csv -NTI -Append <<<<
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBind
ingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
ands.ExportCsvCommandTuesday, May 22, 2018 9:26 AM -
You are using PS2 which should no longer be used for security reasons. Please upgrade to the latest version of PowerShell.
\_(ツ)_/
Tuesday, May 22, 2018 9:28 AM -
I'm running the upgrade now...
I think I know whats happening. Its not skipping the lines in my text document as i first thought. The problem is in the Excel document, its not appending , its overwriting which is why I only see the one entry in the file.
How can i get it to append to excel properly please
Tuesday, May 22, 2018 9:49 AM -
What Excel? There is no Excel being used here. Export-Csv is a PowerShell CmdLet that creates a text file. "Append" only exists in PowerShell 3 or later versions.
\_(ツ)_/
Tuesday, May 22, 2018 9:52 AM -
I solved it :-)
By using out-file -append C:\temp\ADreport.csv as opposed to export-csv
Tuesday, May 22, 2018 10:19 AM -
Thanks for your help :-)Tuesday, May 22, 2018 10:19 AM
-
But now you don't have a usable CSV file.
\_(ツ)_/
Tuesday, May 22, 2018 10:23 AM -
The CSV is usable, I'm outputting the results to CSV (Excel)
| out-file -append C:\temp\ADreport.csv
Wednesday, May 23, 2018 8:59 AM -
Out-File does not create a CSV file. It creates a plain text file with no delimiters.
\_(ツ)_/
Wednesday, May 23, 2018 11:29 AM