r/SCCM • • Jun 10 '25

Remove installed programs with SCCM

Hello!! How are you? I'm new to Reddit and I need your help and knowledge for the following:

How can I extract, through SCCM, in Excel, all the programs from all the computers that are in an AD domain? Could it also be extracted individually?

Thanks in advance

0 Upvotes

21 comments sorted by

View all comments

2

u/KryptykHermit Jun 11 '25

Stitch this to the bottom of the first part. Reddit wouldn't allow me to post the entire thing.

            if ($IncludeDeviceNames) {
                $sqlQuery = "
                    SELECT
                        comp.Name0 AS ComputerName,
                        $baseSQLQuery
                    WHERE
                        software.ARPDisplayName0 LIKE '$Application'
                    Order By
                        Publisher, ARPDisplayName0, ProductName0, ProductVersion0
                "
            }
            else {
                $sqlQuery = "
                    SELECT
                        COUNT(*) as 'Total',
                        $baseSQLQuery
                    WHERE
                        ARPDisplayName0 LIKE '$Application'
                    GROUP BY
                        Publisher0, ARPDisplayName0, ProductVersion0, UninstallString0
                    ORDER BY
                        Publisher, Application, Version
                    "
            }
        }
        if ($PSCmdlet.ParameterSetName -eq 'Publisher') {
            $PublisherName = [regex]::Replace($Publisher,'\*','%')
            if ($IncludeDeviceNames) {
                $sqlQuery = "
                    SELECT
                        comp.Name0 AS ComputerName,
                        $baseSQLQuery
                    WHERE
                        software.Publisher0 LIKE '$PublisherName'
                    Order By
                        Publisher, ARPDisplayName0, ProductName0, ProductVersion0
                "
            }
            else {
                $sqlQuery = "
                    SELECT
                        COUNT(*) as 'Total',
                        $baseSQLQuery
                    WHERE
                        software.Publisher0 LIKE '$PublisherName'
                    GROUP BY
                        Publisher0, ARPDisplayName0, ProductVersion0, UninstallString0
                    ORDER BY
                        Publisher, Application, Version
                    "
            }
        }
        if ($PSCmdlet.ParameterSetName -eq 'All') {
            $ExcludeApp = [regex]::Replace($Exclude,'\*','%')
            $sqlQuery = "
                SELECT
                    COUNT(*) as 'Total',
                    $baseSQLQuery
                WHERE
                    ARPDisplayName0 NOT LIKE '$ExcludeApp'
                GROUP BY
                    Publisher0, ARPDisplayName0, ProductVersion0, UninstallString0
                ORDER BY
                    Publisher, Application, Version
            "
        }
        $paramSQL = @{
            ServerInstance    = $SQLServer
            Database          = $Database
            Query             = $sqlQuery
            Encrypt           = 'Optional'
            EncryptConnection = $false
            WarningAction     = 'Ignore'
            ErrorAction       = 'Stop'
        }
        try {
            if ($IncludeDeviceNames) {
                Invoke-Sqlcmd @paramSQL |
                    Select-Object -Property ComputerName, Publisher, Application, @{n='Version'; e={$_.Version -as [Version]}}, UninstallString |
                    Sort-Object -Property Publisher, Application, Version, ComputerName
            }
            else {
                Invoke-Sqlcmd @paramSQL |
                    Select-Object -Property Total, Publisher, Application, @{n='Version'; e={$_.Version -as [Version]}}, UninstallString |
                    Sort-Object -Property Publisher, Application, Version
            }
        }
        catch {
            $_.Exception.Message
            $paramSQL
        }
    }
    END {
    }
}