Skip to content

Instantly share code, notes, and snippets.

@najashark
Created March 14, 2019 06:48
Show Gist options
  • Save najashark/cf66ece97c61fc4b5440e49bdb9cc1c7 to your computer and use it in GitHub Desktop.
Save najashark/cf66ece97c61fc4b5440e49bdb9cc1c7 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
list Installed Applications in windows.
.DESCRIPTION
Get-InstalledApplication can list all the installed program in windows. make sure use Import-Module
.PARAMETER outpath
path to output folder, dot (.) for current folder
.PARAMETER ComputerName
A remote or local computer nam to scan
.PARAMETER OutputType
Output type range from CSV, GridView, Console
.EXAMPLE 1: OutputType CSV and Output path E:\Output
PS P:\> Get-InstalledApplication -Computername hqdbsp18 -OutputType CSV -outpath E:\OutPut
.EXAMPLE 2: OutputType GridView
PS P:\>Get-InstalledApplication -Computername hqdbsp18 -OutputType GridView
.EXAMPLE 3: OutputType Console
PS P:\>Get-InstalledApplication -Computername hqdbsp18 -OutputType Console
.EXAMPLE 4: Invalid OutputType
PS P:\> Get-InstalledApplication -Computername hqdbsp18 -OutputType Grid
.EXAMPLE 5: Loop through each system to pull the software list
foreach($srv in Get-Content E:\Server.txt)
{
Get-InstalledApplication -Computername $srv -OutputType CSV -outpath E:\OutPut
}
#>
Function Get-InstalledApplication
{
Param(
[Parameter(Mandatory=$true)]
[string[]]$Computername,
[String[]]$OutputType,
[string[]]$outpath
)
#Registry Hives
$Object =@()
$excludeArray = ("Security Update for Windows",
"Update for Windows",
"Update for Microsoft .NET",
"Security Update for Microsoft",
"Hotfix for Windows",
"Hotfix for Microsoft .NET Framework",
"Hotfix for Microsoft Visual Studio 2007 Tools",
"Microsoft Visual C++ 2010",
"cwbin64a",
"Hotfix")
[long]$HIVE_HKROOT = 2147483648
[long]$HIVE_HKCU = 2147483649
[long]$HIVE_HKLM = 2147483650
[long]$HIVE_HKU = 2147483651
[long]$HIVE_HKCC = 2147483653
[long]$HIVE_HKDD = 2147483654
Foreach($EachServer in $Computername){
$Query = Get-WmiObject -ComputerName $Computername -query "Select AddressWidth, DataWidth,Architecture from Win32_Processor"
foreach ($i in $Query)
{
If($i.AddressWidth -eq 64){
$OSArch='64-bit'
}
Else{
$OSArch='32-bit'
}
}
Switch ($OSArch)
{
"64-bit"{
$RegProv = GWMI -Namespace "root\Default" -list -computername $EachServer| where{$_.Name -eq "StdRegProv"}
$Hive = $HIVE_HKLM
$RegKey_64BitApps_64BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
$RegKey_32BitApps_64BitOS = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$RegKey_32BitApps_32BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
$RegKey_ProfileList = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
#############################################################################
# Get SubKey profile names
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_ProfileList)
if ($SubKeys.ReturnValue -eq 0)
{ # Loop through all returned subkeys
ForEach ($SID in $SubKeys.sNames)
{
$HIVE = $HIVE_HKLM
$SubKey = "$RegKey_ProfileList\$SID"
$UserPath = ($RegProv.GetStringValue($Hive, $SubKey, "ProfileImagePath")).sValue
Write-Output $UserPath,$SID
$Object += New-Object PSObject -Property @{UserPath = $UserPath;SID= $SID;}
# Get SubKey names HKU
$HIVE = $HIVE_HKU
$SubKeys = $RegProv.EnumKey($HIVE ,"$SID\$RegKey_64BitApps_64BitOS")
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{ # Loop through all returned subkeys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$SID\$RegKey_64BitApps_64BitOS\$Name"
$AppName = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayName")).sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
$InstallDate = ($RegProv.GetStringValue($Hive, $SubKey, "InstallDate")).sValue
$donotwrite = $false
if($AppName.length -gt "0"){
Foreach($exclude in $excludeArray)
{
if($AppName.StartsWith($exclude) -eq $TRUE)
{
$donotwrite = $true
break
}
}
if ($donotwrite -eq $false)
{
$Object += New-Object PSObject -Property @{
Application = $AppName;
Architecture = "64-BIT";
ServerName = $EachServer;
Version = $Version;
Publisher= $Publisher;
InstallDate= $InstallDate;
User= $UserPath;
}
}
}
}}
}
}
#############################################################################
# Get SubKey names
$HIVE = $HIVE_HKLM
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_64BitApps_64BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{ # Loop through all returned subkeys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_64BitApps_64BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
$InstallDate = ($RegProv.GetStringValue($Hive, $SubKey, "InstallDate")).sValue
$donotwrite = $false
if($AppName.length -gt "0"){
Foreach($exclude in $excludeArray)
{
if($AppName.StartsWith($exclude) -eq $TRUE)
{
$donotwrite = $true
break
}
}
if ($donotwrite -eq $false)
{
$Object += New-Object PSObject -Property @{
Application = $AppName;
Architecture = "64-BIT";
ServerName = $EachServer;
Version = $Version;
Publisher= $Publisher;
InstallDate= $InstallDate;
User= "Local Machine";
}
}
}
}}
#############################################################################
$HIVE = $HIVE_HKLM
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_32BitApps_64BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{
# Loop Through All Returned SubKEys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_32BitApps_64BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
$InstallDate = ($RegProv.GetStringValue($Hive, $SubKey, "InstallDate")).sValue
$donotwrite = $false
if($AppName.length -gt "0"){
Foreach($exclude in $excludeArray)
{
if($AppName.StartsWith($exclude) -eq $TRUE)
{
$donotwrite = $true
break
}
}
if ($donotwrite -eq $false)
{
$Object += New-Object PSObject -Property @{
Application = $AppName;
Architecture = "32-BIT";
ServerName = $EachServer;
Version = $Version;
Publisher= $Publisher;
InstallDate= $InstallDate;
User= "Local Machine";
}
}
}
}
}
} #End of 64 Bit
######################################################################################
###########################################################################################
"32-bit"{
$RegProv = GWMI -Namespace "root\Default" -list -computername $EachServer| where{$_.Name -eq "StdRegProv"}
$Hive = $HIVE_HKLM
$RegKey_32BitApps_32BitOS = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
#############################################################################
# Get SubKey names
$SubKeys = $RegProv.EnumKey($HIVE, $RegKey_32BitApps_32BitOS)
# Make Sure No Error when Reading Registry
if ($SubKeys.ReturnValue -eq 0)
{ # Loop Through All Returned SubKEys
ForEach ($Name in $SubKeys.sNames)
{
$SubKey = "$RegKey_32BitApps_32BitOS\$Name"
$ValueName = "DisplayName"
$ValuesReturned = $RegProv.GetStringValue($Hive, $SubKey, $ValueName)
$AppName = $ValuesReturned.sValue
$Version = ($RegProv.GetStringValue($Hive, $SubKey, "DisplayVersion")).sValue
$Publisher = ($RegProv.GetStringValue($Hive, $SubKey, "Publisher")).sValue
$InstallDate = ($RegProv.GetStringValue($Hive, $SubKey, "InstallDate")).sValue
if($AppName.length -gt "0"){
$Object += New-Object PSObject -Property @{
Application = $AppName;
Architecture = "32-BIT";
ServerName = $EachServer;
Version = $Version;
Publisher= $Publisher;
InstallDate= $InstallDate;
User= "Local Machine";
}
}
}}
}#End of 32 bit
} # End of Switch
}
#$AppsReport
#$column1 = @{expression="ServerName"; width=15; label="Name"; alignment="left"}
$column1 = @{expression="User"; width=20; label="Name"; alignment="left"}
$column2 = @{expression="Architecture"; width=10; label="32/64 Bit"; alignment="left"}
$column3 = @{expression="Application"; width=80; label="Application"; alignment="left"}
$column4 = @{expression="Version"; width=20; label="Version"; alignment="left"}
$column5 = @{expression="Publisher"; width=30; label="Publisher"; alignment="left"}
$column6 = @{expression="InstallDate"; width=15; label="InstallDate"; alignment="left"}
if ($outputType -eq "Console")
{
"#"*80
"Installed Software Application Report"
"Number of Installed Application count : $($object.count)"
"Generated $(get-date)"
"Generated from $(gc env:computername)"
"#"*80
$object |Format-Table $column1, $column2, $column3 ,$column4, $column5, $column6
}
elseif ($OutputType -eq "GridView")
{
$object|Out-GridView
}
elseif ($OutputType -eq "CSV")
{
[string]$FileDS = Get-Date -Format "yyyyMMdd"
[string]$outFile = "${outpath}\${computername}_${FileDS}.csv"
New-Item -ItemType file $outfile -Force
$object | export-csv -path $outfile
}
else
{
write-host " Invalid Output Type $OutputType"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment