Scripts/Get-InstalledSoftware.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
Function Get-InstalledSoftware
{
  <#
      .SYNOPSIS
      "Get-InstalledSoftware" collects all the software listed in the Uninstall registry.
 
      .DESCRIPTION
      Add a more complete description of what the function does.
 
      .PARAMETER SortList
      Allows you to sort by Name, Installed Date or Version Number. 'InstallDate' or 'DisplayName' or 'DisplayVersion'
 
      .PARAMETER SoftwareName
      This wil provide the installed date, version, and name of the software in the "value". You can use part of a name or two words, but they must be in quotes. Mozil or "Mozilla Firefox"
 
      .PARAMETER File
      Future Use: Will be used to send to a file instead of the screen.
 
      .EXAMPLE
      Get-InstalledSoftware -SortList DisplayName
 
      InstallDate DisplayVersion DisplayName
      ----------- -------------- -----------
      20150128 6.1.1600.0 Windows MultiPoint Server Log Collector
      02/06/2007 3.1 Windows Driver Package - Silicon Labs Software (DSI_SiUSBXp_3_1) USB (02/06/2007 3.1)
      07/25/2013 10.30.0.288 Windows Driver Package - Lenovo (WUDFRd) LenovoVhid (07/25/2013 10.30.0.288)
 
 
      .EXAMPLE
      Get-InstalledSoftware -SoftwareName 'Mozilla Firefox',Green,vlc
 
      Installdate DisplayVersion DisplayName
      ----------- -------------- -----------
      69.0 Mozilla Firefox 69.0 (x64 en-US)
      20170112 1.2.9.112 Greenshot 1.2.9.112
      2.1.5 VLC media player
 
      .NOTES
      Place additional notes here.
 
      .LINK
      https://github.com/KnarrStudio/ITPS.OMCS.Tools
 
 
      .OUTPUTS
      To the screen until the File parameter is working
 
  #>


    [cmdletbinding(DefaultParameterSetName = 'SortList',SupportsPaging = $true)]
  Param(
    
    [Parameter(Mandatory = $true,HelpMessage = 'At least part of the software name to test', Position = 0,ParameterSetName = 'SoftwareName')]
    [String[]]$SoftwareName,
    [Parameter(ParameterSetName = 'SortList')]
    [Parameter(ParameterSetName = 'SoftwareName')]
    [ValidateSet('InstallDate', 'DisplayName','DisplayVersion')] 
    [String]$SortList = 'InstallDate'
    
  )
  
  Begin { 
    $SoftwareOutput = @()
    $InstalledSoftware = (Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*)
  }
  
  Process {
    Try 
    {
      if($SoftwareName -eq $null) 
      {
        $SoftwareOutput = $InstalledSoftware |
        #Sort-Object -Descending -Property $SortList |
        Select-Object -Property @{
          Name = 'Date Installed'
          Exp  = {
            $_.Installdate
          }
        }, @{
          Name = 'Version'
          Exp  = {
            $_.DisplayVersion
          }
        }, DisplayName #, UninstallString
      }
      Else 
      {
        foreach($Item in $SoftwareName)
        {
          $SoftwareOutput += $InstalledSoftware |
          Where-Object -Property DisplayName -Match -Value $Item |
          Select-Object -Property @{
            Name = 'Version'
            Exp  = {
              $_.DisplayVersion
            }
          }, DisplayName # , UninstallString
        }
      }
    }
    Catch 
    {
      # get error record
      [Management.Automation.ErrorRecord]$e = $_

      # retrieve information about runtime error
      $info = New-Object -TypeName PSObject -Property @{
        Exception = $e.Exception.Message
        Reason    = $e.CategoryInfo.Reason
        Target    = $e.CategoryInfo.TargetName
        Script    = $e.InvocationInfo.ScriptName
        Line      = $e.InvocationInfo.ScriptLineNumber
        Column    = $e.InvocationInfo.OffsetInLine
      }
      
      # output information. Post-process collected info, and log info (optional)
      $info
    }
  }
  
  End{ 
    Switch ($SortList){
      'DisplayName' 
      {
        $SoftwareOutput |
        Sort-Object -Property displayname
      }
      'DisplayVersion' 
      {
        $SoftwareOutput |
        Sort-Object -Property 'Version'
      }
      'UninstallString'
      {

      }
      default  
      {
        $SoftwareOutput |
        Sort-Object -Property 'Date Installed'
      } #'InstallDate'
      
    }
  }
}

#
# Get-InstalledSoftware -SortList InstallDate | select -First 10 #| Format-Table -AutoSize
# Get-InstalledSoftware -SoftwareName 'Mozilla Firefox',vlc, Acrobat
# Get-InstalledSoftware
 

# SIG # Begin signature block
# MIID/AYJKoZIhvcNAQcCoIID7TCCA+kCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU/0DlqtO3Ha4bduESR+vsY9oZ
# j1KgggIRMIICDTCCAXagAwIBAgIQapk6cNSgeKlJl3aFtKq3jDANBgkqhkiG9w0B
# AQUFADAhMR8wHQYDVQQDDBZLbmFyclN0dWRpb1NpZ25pbmdDZXJ0MB4XDTIwMDIx
# OTIyMTUwM1oXDTI0MDIxOTAwMDAwMFowITEfMB0GA1UEAwwWS25hcnJTdHVkaW9T
# aWduaW5nQ2VydDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAxtuEswl88jvC
# o69/eD6Rtr5pZikUTNGtI2LqT1a3CZ8F6BCC1tp0+ftZLppxueX/BKVBPTTSg/7t
# f5nkGMFIvbabMiYtfWTPr6L32B4SIZayruDkVETRH74RzG3i2xHNMThZykUWsekN
# jAer+/a2o7F7G6A/GlH8kan4MGjo1K0CAwEAAaNGMEQwEwYDVR0lBAwwCgYIKwYB
# BQUHAwMwHQYDVR0OBBYEFGp363bIyuwL4FI0q36S/8cl5MOBMA4GA1UdDwEB/wQE
# AwIHgDANBgkqhkiG9w0BAQUFAAOBgQBkVkTuk0ySiG3DYg0dKBQaUqI8aKssFv8T
# WNo23yXKUASrgjVl1iAt402AQDHE3aR4OKv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