Home > Mobile >  In Powershell, how do I convert an array of PSObjects to an array of Strings?
In Powershell, how do I convert an array of PSObjects to an array of Strings?

Time:06-10

My code is as follows:

    #Creating object with data name properties
$myData = New-Object -TypeName psobject
$myData | Add-Member -MemberType NoteProperty -Name Name -Value $null
$myData | Add-Member -MemberType NoteProperty -Name Bot -Value $null
$myData | Add-Member -MemberType NoteProperty -Name PDD -Value $null
$myData | Add-Member -MemberType NoteProperty -Name SD -Value $null
$myData | Add-Member -MemberType NoteProperty -Name Dev -Value $null

#Empty ArrayList to Populate
$InputArray = @()

for ($i = 2; $i -le $rowMax; $i  )
{
    $objTemp = $myData | Select-Object *
    
    #Reading row data
    $objTemp.Name = $sheet.Cells.Item($i,1).Text
    $objTemp.Bot = $sheet.Cells.Item($i,2).Text
    $objTemp.PDD = $sheet.Cells.Item($i,3).Text
    $objTemp.SD = $sheet.Cells.Item($i,4).Text
    $objTemp.Dev = $sheet.Cells.Item($i,5).Text

    $InputArray  = $objTemp
}

foreach ($i in $InputArray)
    {
        if ($i.Name -eq $CurrentName) {
                 #want to convert these name properties to strings
                 $Name = $i.Name
                 $Bot = $i.Bot
                 $PDD = $i.PDD
                 $Dev = $i.Dev
          
                               
        }  
     }

The code above builds the PSobject object with several name properties that are read in from an excel sheet. After that, I am reading in each psobject in $InputArray, and targeting the properties of the current array in that index.

The problem I'm running into is I need to convert the property values (Name, Bot, PDD, SD, Dev) into string values.

I've tried a few methods to no avail, any input would be much appreciated

CodePudding user response:

[string] $Name = $i.Name would store the stringified value of $i.Name in variable $Name, for instance - although it's surprising that that is needed, given that you're accessing a property named .Text on the Excel cell objects.

Generally, there are two basic ways to stringify (convert a value to a string) in PowerShell; assume the following two variable definitions in the examples below:

# Sample values to stringify.
$val = 1.2       # [double]
$coll = 42, 1.2  # array, containing [int] and [double]
  • With PowerShell custom logic: culture-invariant, with additional logic for meaningfully stringifying collections, such as arrays:

    • Via a [string] cast or type constraint:

      [string] 1.2 # -> "1.2", irrespective of the current culture.
      
      # In combination with variables:
      $valAsString = [string] 1.2 # cast
      [string] $valAsString = 1.2 # type-constraint; auto-converts future 
                                  # assignments to [string] too
      
      [string] $coll # -> "42 1.2", i.e. the space-concatenated list
                     #     of the (themselves stringified) elements.
      
    • Via an expandable (interpolating) string, i.e. inside "...":

      # Note: Only simple variable references as used here can *directly*
      #       be embedded in "..."; to embed *expressions or commands*,
      #       you must use $(...), the subexpression operator; e.g.:
      #         "$($val 1)" # -> "2.2"
      "$val"   # same as: [string] $val
      "$coll"  # same as: [string] $coll
      
  • Via the .NET type at hand: potentially culture-sensitive:

    • Explicitly, via its .ToString() method:

      $val.ToString() # -> "1.2" in English cultures,
                      #    "1,2" in French, for instance
      
      $coll.ToString() # -> "System.Object[]", i.e. just the *type name*
      
    • Implicitly, via -f, the format operator:

      'val: {0}' -f $val # -> "val: 1.2" in English cultures,
                         #    "val: 1,2" in French, for instance
      

See this answer for more information.

Also note that PowerShell's flexible type conversions perform stringification on demand, e.g. when passing a non-string to a [string]-typed parameter.


As for what you tried:

Your code can be greatly simplified as follows; the source-code comments provide pointers, but explaining every optimization would be beyond the scope of this answer:

# Let PowerShell collect the loop output in an array for you,
$inputArray = 
  foreach ($i in 2..$rowMax) {
    # Construct and output a [pscustomobject] via a literal.
    # Note: If desired, you could apply [string] casts *here*; e.g.:
    #       Name = [string] $sheet.Cells.Item($i,1).Text      
    [pscustomobject] @{
      Name = $sheet.Cells.Item($i,1).Text
      Bot = $sheet.Cells.Item($i,2).Text
      PDD = $sheet.Cells.Item($i,3).Text
      SD = $sheet.Cells.Item($i,4).Text
      Dev = $sheet.Cells.Item($i,5).Text
    }
  }

# ...

foreach ($i in $InputArray) {
  if ($i.Name -eq $CurrentName) {
      # want to convert these name properties to strings
      [string] $Name = $i.Name
      [string] $Bot = $i.Bot
      [string] $PDD = $i.PDD
      [string] $Dev = $i.Dev
  }
  # ...
}
  • Related