Powershell-抓取已安装软件

function Get-InstalledSoftwares
{
    #
    # Read registry key as product entity.
    #
    $script:lastname=@();
    function ConvertTo-ProductEntity
    {
        param([Microsoft.Win32.RegistryKey]$RegKey)
        $product = '' | select Name,Publisher,Version
        $product.Name =  $_.GetValue("DisplayName")
        $product.Publisher = $_.GetValue("Publisher")
        $product.Version =  $_.GetValue("DisplayVersion")

        if($product.Name  -in $script:lastname){            
        }
        else{
            if(-not [string]::IsNullOrEmpty($product.Name) -and -not [string]::IsNullOrEmpty($product.Publisher)){
                $product
                $script:lastname=$script:lastname+$_.GetValue("DisplayName")
            }
            
        }
        

        
    }

    $UninstallPaths = @(,
    # For local machine.
    'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
    # For current user.
    'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall')

    # For 32bit softwares that were installed on 64bit operating system.
    if([Environment]::Is64BitOperatingSystem) {
        $UninstallPaths += 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
    }


    $UninstallPaths | foreach {
        Get-ChildItem $_ |foreach {
            ConvertTo-ProductEntity -RegKey $_
        }
    }
}


Get-InstalledSoftwares
posted @ 2021-06-07 15:39  JinweiChang  阅读(83)  评论(0编辑  收藏  举报