user1736786
user1736786

Reputation: 55

Concatenate and aggregate multiple strings together in Powershell

I'm trying to Run Advertised Programs using PowerShell

$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
    | Select-Object -Property PKG_Manufacturer, PKG_Name, PKG_MIFVersion

The output will be:

PKG_Manufacturer PKG_Name PKG_MIFVersion
---------------- -------- --------------
Microsoft        Word     v1234
Google           Chrome   v987
Microsoft        Excel    v987
etc

How do I concatenate it into a string? I tried this:

[string[]]$result = $tpObject.PKG_Manufacturer + $tpObject.PKG_Name + " - " + $tpObject.PKG_MIFVersion  
$result

But it display all the PKG_Manufacturer, then PKG_Name, then PKG_MIFVersion
I would like it to display this, Microsoft Word - v1234 as a string?
Any suggestions or comments would be greatly appreciated.

tks

Upvotes: 1

Views: 3083

Answers (3)

user6811411
user6811411

Reputation:

$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
$tpobject | ForEach-Object{
    "{0} {1} - {2}" -f $_.PKG_Manufacturer, $_.PKG_Name, $_.PKG_MIFVersion
}

See details of the -f format operator

Sample output:

Microsoft Word - v1234
Google Chrome - v987
Microsoft Excel - v987

Upvotes: 0

ShanayL
ShanayL

Reputation: 1247

Give this a try:

$result=@()
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution | %{
$result += "$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)"}
$result

Upvotes: 1

wp78de
wp78de

Reputation: 18950

I guess what you want is something like this

$list = New-Object 'System.Collections.Generic.List[string]'
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
    | ForEach-Object $list.Add("$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)")

To concatenate and aggregate the values in a string array you have several options.

First, you can concatenate a string:

  1. You can concat strings in PowerShell by using the + operator (as applied by you)
  2. You can use the -f formatting approach (like in C#) "My PC {0} has {1} MB of memory." -f "L001", "4096"
  3. Or you can use a double quoted string with variables $x = "Max"; Write-Output "I'm $x" (Hind: Sometimes you need sub-expressions expansion, as shown in my example above. See the Windows PowerShell Language Specification Version 3.0, p34).

Second, you can aggregate your results:

  1. Using a dynamically-typed array $testArray = @() with $testArray += $_
  2. Using a typed array [string[]]$testArray = [string[]] also with += as shown below.
  3. Using a generic List $list = New-Object 'System.Collections.Generic.List[string]' with the Add-Method $list.Add($_) And there is more...

In general, I would try to stay with the PS Objects as long as possible.
Aggregating stuff in a string array to process it later is too much (C#) programmer thinking.

In your Code:
1. You cannot add a new array element with the = operator, use += instead:

$testArray = @()
$tempArray = "123", "321", "453"
$tempArray | ForEach {$testArray += $item }
$testArray

Upvotes: 0

Related Questions