I'm trying to get a bunch of Active Directory attributes using PowerShell for a list of users. I'm importing the list of users into a variable and then use a ForEach loop to go through each user. Here's what the script looks like so far:
$Users=Get-Content "C:\Project\NewUSers\FirstBatch.txt"
$UserInfo = foreach($User in $Users)
{
Get-ADUser $User -properties DisplayName,PasswordLastSet,whencreated | Select-Object -ExpandProperty DisplayName,PasswordLastSet,WhenCreated
}
$UserInfo
However, when I run the script, I get the following error message:
Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported. At line:4 char:104 + ... ExpandProperty DisplayName,PasswordLastSet + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SelectObjectCommand
The script does run when I only retrieve a signal attribute. So if I were to change the code to this, it'll work and give me the value to those attributes:
Get-ADUser $User -properties DisplayName,PasswordLastSet,whencreated | Select-Object -ExpandProperty PasswordLastSet
Would anyone be able to point out where the issue may be? Any other suggestion would also be welcome. Thank you very much!