Powershell to combine multiple values


  • Hi there,

    I need help with the following for my project in sharepoint. I have an external list with values like:

    ToolId                   ProductId

    T001                       ProductA

    T001                       ProductB

    T002                       ProductA

    T002                       ProductC

    I use powershell to exort that list into a CSV. I need to manipulate the external list and my final result needs to be like that:

    ToolId                   ProductId

    T001                       ProductA; ProductB

    T002                       ProductA; ProductC

    This is my PS code so far:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
    #Get the context
    $ctx = Get-SPServiceContext https://path
    #Get the scope
    $scope = new-object Microsoft.SharePoint.SPServiceContextScope $ctx
    #Get the target site collection 
    $webTarget = Get-SPWeb -identity "https://path"
    #Get the Target List 
    $list = $webTarget.Lists["Name of List"] 
    #Array to Hold Result - PSObjects 
    $ListItemCollection = @() 
     #Get All List items 
     $list.Items |  Where-Object { $_["ToolId"] -ne ''} | foreach { 
     $ExportItem = New-Object PSObject
     $ExportItem | Add-Member -MemberType NoteProperty -name "ToolId" -value $_["ToolId"]
     $ExportItem | Add-Member -MemberType NoteProperty -name "ProductId" -value $_["ProductId"]  
     #Add the object with property to an Array 
     $ListItemCollection += $ExportItem
     #Export the result Array to CSV file 
     $ListItemCollection | Export-CSV "\\path\test.csv" -NoTypeInformation                        
    #Dispose the web Object 

    I’m having problems with PSObjects and hash tables to get all the items of the same ToolId and collect all the ProductId’s relevant to them to populate my CSV. Any help highly appreciated.

    • Edited by Lukas_UK Thursday, October 1, 2015 1:41 PM
    Thursday, October 1, 2015 1:40 PM