PowerSHell object with comma concatenation

MS Techie 2,686 Reputation points
2021-09-21T10:31:27.667+00:00

i want to concatenate objects using Powershell , if not null and add comma after that.

Example
$Result = $object1 , $object2, $object3, $Object4

if any of the objects are NULL , then i want to remove them from the above statement .. Say suppose $object4 is null, then my statement shoiuld be
$Result = $object1,$object2,Object3 (no comma in end )

Say suppose $object 2 is NULL, then my statement should be
$Result = $object1, $object3,Object4

Basically i want to omit the object which is null in the above statement .. i know if(obj) will check if object is null... but i have to write lengthy queries for it.. any powershell ternary or Coalesce or any othe way to achieve it.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. MotoX80 32,641 Reputation points
    2021-09-21T12:36:14.1+00:00

    If you give your variables identifiable names you could do something like this.

    cls
    $object1 = "aaaa"
    $object2 = $null
    $object3 = "cccc" 
    $object4 = $null
    $result = @()
    Get-Variable -Name 'object*' | foreach {    # get our objects
        if ($_.Value) {
            "Appending {0}" -f $_.name 
            $result += $_.Value               # append this object to the result
        }
    }
    ""
    "Result is type {0} with a count of {1}" -f $Result.GetType().BaseType, $Result.count
    ""
    $Result
    

  2. Rich Matheisen 45,671 Reputation points
    2021-09-21T14:59:05.317+00:00

    Your description of your objective (and your previous comment) is a bit confusing.

    You begin by giving an example of creating an array of objects ($Result = $object1 , $object2, $object3, $Object4), but you then ask how to change the STATEMENT, and not the contents of the array, into something different.

    This sounds as if you're trying to create a string that will be used in an Invoke-Expression.

    Do either one of these examples produce the result you want? If not, I think you need to further explain what you're attempting to do.

    # Remove $null items from array
    # and create new array
    $x = $null,"aaaa","cccc",$null
    $x = $x | Where-Object {$_ -ne $null}
    
    # Join items of array and, if they aren't $null,
    # create a comma separated string
    $x = $null,"aaaa","cccc",$null
    $x = ($x | Where-Object {$_ -ne $null}) -join ","
    
    0 comments No comments

  3. Rich Matheisen 45,671 Reputation points
    2021-09-21T20:11:59.53+00:00

    Never underestimate the power of an idle mind!

    $object1 = "aaaa"
    $object2 = $null
    $object3 = "cccc" 
    $object4 = $null
    $result = @()
    $n =    Get-Variable -Name 'object*' | 
                ForEach-Object {
                    if ($_.Value) {
                        $_.Name
                    }
                }
    $x = "{0} = {1}" -f "Result", ($n -join ",")
    

    That yields this string in the variable $x:

    $Result = $object1,$object3


  4. Limitless Technology 39,506 Reputation points
    2021-09-23T10:40:24.027+00:00

    Hello MSTechie,

    The Join-String cmdlet joins or combines, text from pipeline objects into a single string.

    If no parameters are specified, the pipeline objects are converted to a string and joined with the default separator $OFS.

    By specifying a property name, the property's value is converted to a string and joined into a string.

    Instead of a property name, a script block can be used. The script block's result is converted to a string before it's joined to form the result. It can either combine the text of an object's property or the result of the object that was converted to a string.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/join-string?view=powershell-7.1

    ----------------------------------------------------------------------------------------------------------------------------

    Hope this answers all your queries, if not please do repost back.
    If an Answer is helpful, please click "Accept Answer" and upvote it : )