Powershell Universal Export Selected Items from table only

Hi everyone,

I’m brand new to PowerShell Universal. I’m not a Powershell expert, but i’ve already written various PowerShell scripts, but I’m looking for a way to make them available via a website, so I’m currently testing PowerShell Universal.

I’ve created a first app that extracts all deactivated user accounts from Active Directory. Now, using the export function, I can either export all or, if I set a filter on a column, only the current view. What I can’t do is export only selected rows.
How can i do that?

Someone has already asked this question here, but there’s no answer.
https://forums.ironmansoftware.com/t/example-of-exporting-selected-rows-of-a-new-udtable-using-the-onexport-parameter/12203

In general, I’m still missing some understandable example code from practice.

New-UDApp -Content { 
$Data = @()
$Data.Clear()
$SearchBase0 = "OU=ABC,OU=ABC,DC=DOMAIN,DC=LOCAL"
$OU = "USER"
$Filter = @('MYFILTER','MYFILTER2','MYFILTER3')
$OUs = Get-ADOrganizationalUnit -SearchBase $SearchBase0 -SearchScope OneLevel -Filter * | Where-Object {$_.Name -notin $Filter} | Select-Object Name

foreach ($item in $OUs){
    $Amt = $item.Name
    $SearchBase1 = "OU="+$OU+",OU="+$Amt+",OU=ABC,OU=ABC,DC=DOMAIN,DC=LOCAL"

    if ($Amt -like "EGAL") {
        $Users = Get-ADUser -SearchBase $SearchBase1 -SearchScope Subtree -Filter * -Properties Surname, GivenName, SamAccountName, Department, Enabled, DistinguishedName | Where-Object {$_.DistinguishedName -notlike $Filter -and ($_.Enabled -eq $false) } | Select-Object Surname, GivenName, SamAccountName, Department        
        $Data = $Data +$Users 

    } else {
        $Users = Get-ADUser -SearchBase $SearchBase1 -SearchScope OneLevel -Filter * -Properties Surname, GivenName, SamAccountName, Department, Enabled, DistinguishedName | Where-Object {$_.DistinguishedName -notlike $Filter -and ($_.Enabled -eq $false) } | Select-Object Surname, GivenName, SamAccountName, Department
        $Data = $Data +$Users 
    }
}

$Columns = @(
     New-UDTableColumn -Property Surname -Title "Vorname" -ShowFilter -IncludeInExport
     New-UDTableColumn -Property GivenName -Title "Nachname" -ShowFilter -IncludeInExport
     New-UDTableColumn -Property SamAccountName -Title "Anmeldekürzel" -ShowFilter -IncludeInExport
     New-UDTableColumn -Property Department -Title "Amt" -ShowFilter -IncludeInExport -DefaultSortColumn
 )

New-UDTable -Id 'table1' -Data $Data -Columns $Columns -ShowFilter -ShowSort -ShowPagination -ShowSelection -PageSize 100 -ShowExport

}

Product: PowerShell Universal
Version: 5.5.2

Hi,

I don’t know if this is the best solution but this works for me

# keep your code
...
$Toolbar = {
   New-UDButton -Text "Export selected Rows" -OnClick {
      $TableData = Get-UDElement -id "table1"
      $TableData.selectedRows | Export-CSV -Path "enter your Path here" -NoTypeInformation -Delimiter ";"
   }
}

New-UDTable -Id 'table1' -Data $Data -Columns $Columns -ShowFilter -ShowSort -ShowPagination -ShowSelection -PageSize 100 -ShowExport -ToolbarContent $Toolbar

If you want to download a file via browser you should have a look at

1 Like

Thanks for your post!
Of course, I’d like to be able to download the selected rows as PDF or XLSX files via the browser - that is very comfortable.

Unfortunately, the link to interaction#downloads doesn’t really help me (as a beginner).

I had a look at Data Grids instead of Tables, there you can choose to only export selected rows.

They work similar as tables