0
votes

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!

3

3 Answers

0
votes

You Can't Expand multiple properties, You can create an array and add those info into it, like this:

$Array = @() ## this array will hold all the data
$Users=Get-Content "C:\Project\NewUSers\FirstBatch.txt"

foreach($User in $Users) 
{
  $User = Get-ADUser $User -properties DisplayName,PasswordLastSet,whencreated 
  $Result = "" | Select DisplayName,PasswordLastSet,WhenCreated ## this will create a single user data object
  $Result.DisplayName = $User.DisplayName
  $Result.PasswordLastSet = $User.PasswordLastSet
  $Result.WhenCreated = $User.WhenCreated
  $Array += $Result ## this will add the single user data into the array
}

$Array
0
votes

You shouldn't need to use -ExpandObject with these properties, they're all simple types like string and datetime. -ExpandObject is used to display all the properties of an object or convert the contents of an array into a string.

https://technet.microsoft.com/en-us/library/hh849895.aspx

As always with powershell there are many ways to achieve the same result, the following to examples will achieve the desired outcome.

Using the pipeline

$Users=Get-Content "C:\Project\NewUSers\FirstBatch.txt"
$UserInfo = $Users | foreach-object {Get-ADUser $_ -properties DisplayName,PasswordLastSet,whencreated} | Select DisplayName, PasswordLastSet, WhenCreated
$UserInfo

Using standard iteration

$Users = Get-Content "C:\Project\NewUSers\FirstBatch.txt"
$UserInfo = foreach($User in $Users) 
{
  Get-ADUser $User -properties DisplayName,PasswordLastSet,whencreated | Select-Object DisplayName,PasswordLastSet,WhenCreated
}
$UserInfo
0
votes

You can only set a single object or an array for the switch -ExpandProperty. For the specified parameters (DisplayName,PasswordLastSet,WhenCreated) you don't need to specify ExpandProperty switch because they are single value properties.

Run your code but without ExpandProperty:

 $Users=Get-Content "C:\Project\NewUSers\FirstBatch.txt"
$array = @()
foreach($User in $Users) 
{
  $aduser = Get-ADUser $User -properties DisplayName,PasswordLastSet,whencreated 
  $array += $aduser
}

$array| select DisplayName,PasswordLastSet,whencreated |export-csv -Path "C:\Project\NewUSers\users.csv" -NoTypeInformation