winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Get-WinGetPackage.ps1 (4862B)


      1 Function Get-WinGetPackage{
      2     <#
      3         .SYNOPSIS
      4         Gets installed packages on the local system. displays the packages installed on the system, as well as whether an update is available. 
      5         Additional options can be provided to filter the output, much like the search command.
      6         
      7         .DESCRIPTION
      8         By running this cmdlet with the required inputs, it will retrieve the packages installed on the local system.
      9 
     10         .PARAMETER Filter
     11         Used to search across multiple fields of the package.
     12         
     13         .PARAMETER Id
     14         Used to specify the Id of the package
     15 
     16         .PARAMETER Name
     17         Used to specify the Name of the package
     18 
     19         .PARAMETER Moniker
     20         Used to specify the Moniker of the package
     21 
     22         .PARAMETER Tag
     23         Used to specify the Tag of the package
     24         
     25         .PARAMETER Command
     26         Used to specify the Command of the package
     27 
     28         .PARAMETER Count
     29         Used to specify the maximum number of packages to return
     30         
     31         .PARAMETER Exact
     32         Used to specify an exact match for any parameters provided. Many of the other parameters may be used for case-insensitive substring matches if Exact is not specified.
     33 
     34         .PARAMETER Source
     35         Name of the Windows Package Manager private source. Can be identified by running: "Get-WinGetSource" and using the source Name
     36 
     37         .PARAMETER Header
     38         Used to specify the value to pass as the "Windows-Package-Manager" HTTP header for a REST source.
     39         
     40         .PARAMETER AcceptSourceAgreement
     41         Used to accept any source agreements required by a REST source.
     42 
     43         .EXAMPLE
     44         Get-WinGetPackage -id "Publisher.Package"
     45 
     46         This example expects only a single configured REST source with a package containing "Publisher.Package" as a valid identifier.
     47 
     48         .EXAMPLE
     49         Get-WinGetPackage -id "Publisher.Package" -source "Private"
     50 
     51         This example expects the REST source named "Private" with a package containing "Publisher.Package" as a valid identifier.
     52 
     53         .EXAMPLE
     54         Get-WinGetPackage -Name "Package"
     55 
     56         This example expects the REST source named "Private" with a package containing "Package" as a valid name.
     57     #>
     58 
     59     PARAM(
     60         [Parameter(Position=0)] $Filter,
     61         [Parameter()]           $Name,
     62         [Parameter()]           $Id,
     63         [Parameter()]           $Moniker,
     64         [Parameter()]           $Tag,
     65         [Parameter()]           $Source,
     66         [Parameter()]           $Command,
     67         [Parameter()]           [ValidateRange(1, [int]::maxvalue)][int]$Count,
     68         [Parameter()]           [switch]$Exact,
     69         [Parameter()]           [ValidateLength(1, 1024)]$Header,
     70         [Parameter()]           [switch]$AcceptSourceAgreement
     71     )
     72     BEGIN
     73     {
     74         [string[]]       $WinGetArgs  = @("List")
     75         [WinGetPackage[]]$Result      = @()
     76         [string[]]       $IndexTitles = @("Name", "Id", "Version", "Available", "Source")
     77 
     78         if($Filter){
     79             ## Search across Name, ID, moniker, and tags
     80             $WinGetArgs += $Filter
     81         }
     82         if($PSBoundParameters.ContainsKey('Name')){
     83             ## Search for the Name
     84             $WinGetArgs += "--Name", $Name.Replace("…", "")
     85         }
     86         if($PSBoundParameters.ContainsKey('Id')){
     87             ## Search for the ID
     88             $WinGetArgs += "--Id", $Id.Replace("…", "")
     89         }
     90         if($PSBoundParameters.ContainsKey('Moniker')){
     91             ## Search for the Moniker
     92             $WinGetArgs += "--Moniker", $Moniker.Replace("…", "")
     93         }
     94         if($PSBoundParameters.ContainsKey('Tag')){
     95             ## Search for the Tag
     96             $WinGetArgs += "--Tag", $Tag.Replace("…", "")
     97         }
     98         if($PSBoundParameters.ContainsKey('Source')){
     99             ## Search for the Source
    100             $WinGetArgs += "--Source", $Source.Replace("…", "")
    101         }
    102         if($PSBoundParameters.ContainsKey('Count')){
    103             ## Specify the number of results to return
    104             $WinGetArgs += "--Count", $Count
    105         }
    106         if($Exact){
    107             ## Search using exact values specified (case-sensitive)
    108             $WinGetArgs += "--Exact"
    109         }
    110         if($PSBoundParameters.ContainsKey('Header')){
    111             ## Pass the value specified as the Windows-Package-Manager HTTP header
    112             $WinGetArgs += "--header", $Header
    113         }
    114         if($AcceptSourceAgreement){
    115             ## Accept source agreements
    116             $WinGetArgs += "--accept-source-agreements"
    117         }
    118     }
    119     PROCESS
    120     {
    121         $List = Invoke-WinGetCommand -WinGetArgs $WinGetArgs -IndexTitles $IndexTitles
    122     
    123         foreach ($Obj in $List) {
    124             $Result += [WinGetPackage]::New($Obj) 
    125         }
    126     }
    127     END
    128     {
    129         return $Result
    130     }
    131 }
    132 
    133 Export-ModuleMember -Function Get-WinGetPackage