Share via

PowerSHell object with comma concatenation

MS Techie 2,761 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 for business | Windows Server | User experience | PowerShell
0 comments No comments

4 answers

Sort by: Most helpful
  1. Limitless Technology 40,101 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 : )

    Was this answer helpful?


  2. Rich Matheisen 48,116 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

    Was this answer helpful?


  3. Rich Matheisen 48,116 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 ","
    

    Was this answer helpful?

    0 comments No comments

  4. MotoX80 37,686 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
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.