Notification texts go here Contact Us Buy Now!

Powershell Array Issue when one item found

Problem

#populate a 2D array with NAM and with UserID
$ResultsArray = @(($ADUsersList.name),($ADUsersList.samaccountname),($ADUsersList.enabled))

The issue is that if $ADUsersList contains only 1 object, $ResultsArray will become a flat array instead of a nested array. This is because member-access enumeration applies pipeline-like logic, returning a single value as-is and an array for two or more values.

Solution

#populate a 2D array with NAM and with UserID
$ResultsArray = @(@($ADUsersList.name), @($ADUsersList.samaccountname), @($ADUsersList.enabled))

To ensure that the result of member-access enumeration is an array, use the array-subexpression operator (@(...)) or, preferably, cast it to an array using [array] $ADUsersList.Name.

Alternatively, you can skip the creation of $ResultsArray and directly use the array of objects in $ADUsersList.

#add aduser properties to array, return error and exit if no user found
$ADUsersList = @(Get-ADUser -filter 'surname -like $search' -Properties * | select name,samaccountname,DistinguishedName,Enabled)

if ($ADUsersList.count -le 1){
    Write-host "USER NOT FOUND"
    return
}

# skip the `$ResultsArray` step completely

Write-host "`t NAME `t`t`t USER ID `t`t ACCOUNT ENABLED"
#return list of found users and user ids. Preface with index number to use for selection menu
for ($i = 0; $i -le $ADUsersList.Count; $i++) {
    "[{0}] = {1} `t `t {2} `t `t {3}" -f $i,$ADUserlist[$i].Name,$ADUserlist[$i].SAMAccountName,$ADUserlist[$i].Enabled
}

This solution eliminates the need for the $ResultsArray step and uses the $ADUsersList array directly.

Additional Information

Member-access enumeration applies pipeline-like logic to the property values it retrieves. Therefore, a single value is output as-is, and two or more values are returned as a regular PowerShell array.

To ensure that a member-access enumeration returns an array of values, you must wrap it in @(...) or cast it to [array].

# Create a single-element array.
$ADUsersList = @([pscustomobject] @{ Name = 'J.Doe' })

# Apply member-access enumeration:

# WITHOUT ensuring that that result is an array: 
#  `$ADUsersList.Name` is a single [string], 
#  and [0] extract its first *character*
# -> !! Outputs just 'J'.
$ADUsersList.Name[0]

# WITH ensuring that that result is an array.
#  `[array] $ADUsersList.Name` is a single-element array
#   and [0] extract its first *element*, i.e the contained [string].
# -> OK: 'J.Doe'
([array] $ADUsersList.Name)[0]

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.