none
Retrieving values from Get-Package RRS feed

  • Question

  • I'm currently working on some scripting where I would like to leverage the new Package Manager cmdlets (formerly OneGet, now in v5).  My issue is getting the key/value pairs extracted easily from the result of a Get-Package call.

    Example where I get the information about the Notepad++ install:

    Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller

    Gives me:

    Name                           Version          Source                         Summary
    ----                           -------          ------                         -------
    Notepad++                      6.9.2                                                  

    When I set to a variable, I can see the data I want in a couple of different parts like so:

    $pkgInfo = Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller
    $pkgInfo.Metadata
    

    Which outputs:

    Keys                                                    Values                                Count
    ----                                                    ------                                -----
    {DisplayName, Publisher, VersionMajor, VersionMinor...} {Notepad++, Notepad++ Team, 6, 92...}   139

    Or

    $pkgInfo = Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller
    $pkgInfo.SwidTagText

    Which gives me the output of the data in xml layout

    <?xml version="1.0" encoding="utf-16" standalone="yes"?> <SoftwareIdentity name="Notepad++" version="6.9.2" versionScheme="unknown" xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd"> <Meta DisplayName="Notepad++" Publisher="Notepad++ Team" VersionMajor="6" VersionMinor="92" MajorVersion="6" MinorVersion="92" UninstallString="C:\Program Files (x86)\Notepad++\uninstall.exe" DisplayIcon="C:\Program Files (x86)\Notepad++\notepad++.exe" DisplayVersion="6.9.2" URLInfoAbout="http://notepad-plus-plus.org/" MementoSection_explorerContextMenu="1"

    ... MementoSection_AutoUpdater="1" /> </SoftwareIdentity>

    But that is a string:

    ($pkgInfo.SwidTagText).GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object

    These are essentially the registry entry key/value pairs from the uninstall locations.  I can't for the life of me extract a pair like "InstallDate" and the corresponding date easily from this returned object.


    Wednesday, August 17, 2016 6:20 PM

Answers

  • Hi,

    why not just retrieve the information from the Metadata Hashtable?

    $r = Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller
    $r.Metadata["DisplayName"]
    $r.Metadata["DisplayVersion"]

    Cheers,
    Fred

    Or if you want to process them all:

    $r.Metadata.Keys | %{ $n = $_; new-object psobject -property @{ Key = $N; Value = $r.Metadata[$n] } }


    There's no place like 127.0.0.1


    • Edited by FWN Friday, August 19, 2016 12:17 PM
    • Marked as answer by NAElling Friday, August 19, 2016 4:49 PM
    Friday, August 19, 2016 12:15 PM
  • It is very easy if you look closely at the object
    PS > [xml]$xml=@'
    <?xml version="1.0" encoding="utf-16" standalone="yes"?>
    <SoftwareIdentity
      name="Notepad++"
      version="6.9.2"
      versionScheme="unknown" xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd">
      <Meta
        DisplayName="Notepad++"
        Publisher="Notepad++ Team"
        VersionMajor="6"
        VersionMinor="92"
        MajorVersion="6"
        MinorVersion="92"
        UninstallString="C:\Program Files (x86)\Notepad++\uninstall.exe"
        DisplayIcon="C:\Program Files (x86)\Notepad++\notepad++.exe"
        DisplayVersion="6.9.2"
        URLInfoAbout="http://notepad-plus-plus.org/"
        MementoSection_explorerContextMenu="1"
        MementoSection_AutoUpdater="1" />
    </SoftwareIdentity>
    
    '@
    PS > $xml.SoftwareIdentity.Meta
    
    
    DisplayName                        : Notepad++
    Publisher                          : Notepad++ Team
    VersionMajor                       : 6
    VersionMinor                       : 92
    MajorVersion                       : 6
    MinorVersion                       : 92
    UninstallString                    : C:\Program Files (x86)\Notepad++\uninstall.exe
    DisplayIcon                        : C:\Program Files (x86)\Notepad++\notepad++.exe
    DisplayVersion                     : 6.9.2
    URLInfoAbout                       : http://notepad-plus-plus.org/
    MementoSection_explorerContextMenu : 1
    MementoSection_AutoUpdater         : 1

    PS > $xml.SoftwareIdentity.Meta.DisplayNameNotepad++
     $xml.SoftwareIdentity.Meta.DisplayName

    \_(ツ)_/

    • Marked as answer by NAElling Friday, August 19, 2016 4:49 PM
    Friday, August 19, 2016 1:50 PM

All replies

  • They are metadata used to identify the package.

    [xml]$pkgInfo.SwidTagText

    This converts the string into usable XML


    \_(ツ)_/


    • Edited by jrv Wednesday, August 17, 2016 6:43 PM
    Wednesday, August 17, 2016 6:42 PM
  • Thank you for your reply. I had tried that but the Key/Value pairs are stuck within the <meta> tag and I'd still have to use some kind of string manipulation.  In that case I may as well do string manipulation directly on it without conversion to an [xml] object.  I already had written a module to get me the list of installed software and attributes that I can access easily, but was wanting to use this built-in cmdlet.  I guess I'll have to stick to my module.

    $softInfo = Get-InstalledSoftware -Name "Notepad++"
    $softInfo.DisplayVersion

    Friday, August 19, 2016 12:09 PM
  • Hi,

    why not just retrieve the information from the Metadata Hashtable?

    $r = Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller
    $r.Metadata["DisplayName"]
    $r.Metadata["DisplayVersion"]

    Cheers,
    Fred

    Or if you want to process them all:

    $r.Metadata.Keys | %{ $n = $_; new-object psobject -property @{ Key = $N; Value = $r.Metadata[$n] } }


    There's no place like 127.0.0.1


    • Edited by FWN Friday, August 19, 2016 12:17 PM
    • Marked as answer by NAElling Friday, August 19, 2016 4:49 PM
    Friday, August 19, 2016 12:15 PM
  • It is very easy if you look closely at the object
    PS > [xml]$xml=@'
    <?xml version="1.0" encoding="utf-16" standalone="yes"?>
    <SoftwareIdentity
      name="Notepad++"
      version="6.9.2"
      versionScheme="unknown" xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd">
      <Meta
        DisplayName="Notepad++"
        Publisher="Notepad++ Team"
        VersionMajor="6"
        VersionMinor="92"
        MajorVersion="6"
        MinorVersion="92"
        UninstallString="C:\Program Files (x86)\Notepad++\uninstall.exe"
        DisplayIcon="C:\Program Files (x86)\Notepad++\notepad++.exe"
        DisplayVersion="6.9.2"
        URLInfoAbout="http://notepad-plus-plus.org/"
        MementoSection_explorerContextMenu="1"
        MementoSection_AutoUpdater="1" />
    </SoftwareIdentity>
    
    '@
    PS > $xml.SoftwareIdentity.Meta
    
    
    DisplayName                        : Notepad++
    Publisher                          : Notepad++ Team
    VersionMajor                       : 6
    VersionMinor                       : 92
    MajorVersion                       : 6
    MinorVersion                       : 92
    UninstallString                    : C:\Program Files (x86)\Notepad++\uninstall.exe
    DisplayIcon                        : C:\Program Files (x86)\Notepad++\notepad++.exe
    DisplayVersion                     : 6.9.2
    URLInfoAbout                       : http://notepad-plus-plus.org/
    MementoSection_explorerContextMenu : 1
    MementoSection_AutoUpdater         : 1

    PS > $xml.SoftwareIdentity.Meta.DisplayNameNotepad++
     $xml.SoftwareIdentity.Meta.DisplayName

    \_(ツ)_/

    • Marked as answer by NAElling Friday, August 19, 2016 4:49 PM
    Friday, August 19, 2016 1:50 PM
  • $r = Get-Package -Name "Notepad++" -ProviderName Programs -IncludeWindowsInstaller
    $r.Metadata["DisplayName"]
    $r.Metadata["DisplayVersion"]

    Didn't know you could do that, it's exactly what I was looking for!  This is why I asked here because I knew someone would know a better method.

    I had gone and figured how to get the index references and was going to post that, but this^ method is cleaner and easier. What I did:

    $softInfo.Metadata.values[$softInfo.Metadata.Keys.LocalName.IndexOf("InstallDate")]
    Thanks Fred!

    Friday, August 19, 2016 4:37 PM
  • Thanks for the insight into that xml object usage, I was sure there was a way to do that.  I now have 3 methods:

    
    
    $softInfo = Get-Package -ProviderName Programs -Name "Notepad++" -IncludeWindowsInstaller
    #Fred's Method
    $softInfo.Metadata["InstallDate"]
    #My Method
    $softInfo.Metadata.values[$softInfo.Metadata.Keys.LocalName.IndexOf("InstallDate")]
    #jrv Method
    ([xml]$softInfo.SwidTagText).SoftwareIdentity.Meta.DisplayName


    Friday, August 19, 2016 4:49 PM
  • The Metadata is the easiest method since it is already an object.  You posted XML so I showed that solution.  Use Fred's method as it is more direct.

    I am glad you have a solution.

    I could not find "Notepad++" in the repository for some reason.


    \_(ツ)_/

    Friday, August 19, 2016 5:05 PM