Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:57:50+00:00 2026-06-12T23:57:50+00:00

Powershell unrolling is driving me crazy. I have the following code to retrieve email

  • 0

Powershell unrolling is driving me crazy.

I have the following code to retrieve email addresses from an exchange recipient. I’m using the ArrayList because it is suggested by many people when you want the ability to remove items from the array.

$aliases = New-Object System.Collections.ArrayList
$smtpAddresses = (Get-Recipient $this.DN).EmailAddresses | ?{$_.Prefix.ToString() -eq 'smtp' } 
    foreach ($smtpAddress in $smtpAddresses) {
        $aliases.Add($smtpAddress.SmtpAddress)
    }
return $aliases

The value of $aliases is correct at the end of the function (i.e. will contain x email addresses and is type ArrayList) but after returning it becomes System.Object[] and has 2x entries. There x Int32’s followed by x Strings (i.e. {0, 1, bob@here, bob@there} ). Why does this happen and how to I keep my ArrayList intact? Am I wrong for using ArrayList?

Out of curiosity, with all the questions/problems resulting from PS unrolling, what is its purpose? The big benefit of powershell is that you work directly with objects instead of their textual projections, unfortunately, I never know what kind of object I’m working with – and even when I check, it doesn’t seem to hold its shape for more than a few lines of code.

— Edit
The function is called as part of a PSObject

$get_aliases = { ... }
$obj | Add-Member -MemberType ScriptProperty -Name Aliases -Value $get_aliases -SecondValue $set_aliases
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:57:51+00:00Added an answer on June 12, 2026 at 11:57 pm

    Part of the problem is how the array is being used inside the function. Remember, a function in PowerShell doesn’t actually return anything. It writes objects to the pipeline. Therefore, the return is superfluous, but not actually causing any problems. The use of the Add function is causing the problem because Add returns the index at which the value was added and therefore writes to the pipeline as well.

    function get-myarray
    { 
       $al = New-Object System.Collections.ArrayList
       $al.Add( 0 ) 
       $al.Add( 1 )
       $al.Add( 'me@co.com' )
       $al.Add( 'you.co.com' )
       return $al 
    }
    
    $array = get-myarray
    $array.Count
    8     
    

    Note how the size is 8. What needs to be done is to suppress the writing of what is returned by the Add function. There are a few ways to do this but here is one:

    function get-myarray
    { 
       $al = New-Object System.Collections.ArrayList
       $al.Add( 0 ) | out-null
       $al.Add( 1 )  | out-null
       $al.Add( 'me@co.com' )  | out-null
       $al.Add( 'you.co.com' )  | out-null
       return $al 
    }
    
    $array = get-myarray
    $array.Count
    4 
    

    I don’t believe the use of `ArrayList’ is a wrong one if you want to remove items from it.

    As far as unrolling goes, this deserves a whole other question and has been already addressed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does PowerShell have an equivalent to this command construct from sh (and derivatives): $
My powershell code actually reads data from an XML and stores the specific data
Using PowerShell I can get the directories with the following command: Get-ChildItem -Path $path
In Powershell, suppose I have the following xml: <Users> <User Name=Foo> <Friends> <Friend Name=Bar/>
I have the following powershell script which binds to an active directory OU and
I almost have this Powershell script completed but I am stuck at the last
In PowerShell you can use the Get-WmiObject cmdlet to grab WMI classes. I have
Using powershell how do you create a content sorce that uses a BDC? Documentation
My powershell script takes the following parameter: Param($BackedUpFilePath) The value that is getting passed
Can PowerShell on Windows by itself or using simple shell script, list files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.