Referencing properties of Get-WindowsUpdate object

Hello!

With the PSWindowsUpdate module, the cmdlet Get-WindowsUpdate produces an array of objects of this kind:

PS C:\WINDOWS\system32> $test1 = Get-WindowsUpdate
PS C:\WINDOWS\system32> $test1

ComputerName Status     KB          Size Title
------------ ------     --          ---- -----
myhostname   -------    KBxxxxxx1   66MB
myhostname   -------    KBxxxxxx2    1GB

The available updates can be referenced to as $test1[0] and $test1[1].

PS C:\WINDOWS\system32> $test1[0]

ComputerName Status     KB          Size Title
------------ ------     --          ---- -----
myhostname   -------    KBxxxxxx1   66MB


PS C:\WINDOWS\system32> $test1[1]

ComputerName Status     KB          Size Title
------------ ------     --          ---- -----
myhostname   -------    KBxxxxxx2   1GB

They should have non-empty properties $test1[0].ComputerName, $test1[0].KB and $test1[0].Size, but if I try to print them, there is no output:

PS C:\WINDOWS\system32> $test1[0].KB
PS C:\WINDOWS\system32> $test1[0].KB
PS C:\WINDOWS\system32> $test1[0].Size
PS C:\WINDOWS\system32> $test1[1].Size
PS C:\WINDOWS\system32> $test1[1].ComputerName
PS C:\WINDOWS\system32> $test1[0].ComputerName
PS C:\WINDOWS\system32>

While manually I can copy-paste the KB of an update and apply it through

Get-WindowsUpdate -KBArticleID KBxxxxxx1 -Install

there is no way to automatize this within a script (like the one used in my previous post).

How to reference the KB or the Title properties of such objects? May this be a module-related issue or a PowerShell-related issue?

Note: I am using Windows Powershell 5.1 (run as Administrator). On PowerShell 7.4 (as Administrator) Get-WindowsUpdate produces no output at all (even if the -Verbose option shows Found [2] Updates in post search criteria).

Please pipe the output of Get-WindowsUpdate to Get-Member and post that output here.
Get-WindowsUpdate | Get-Member

EDIT: https://github.com/mgajda83/PSWindowsUpdate/blob/main/PSWindowsUpdate/PSWindowsUpdate.Format.ps1xml
We can see in the project’s repo that they’re using a format file to control the output of Get-WindowsUpdate to format it as a table, even though it displays 5 properties which PS would normally format as a list.
My guess is they’re also renaming some of the properties for that table display. Piping it to Get-Member (gm for short) should tell you what properties are there and their names.

1 Like
PS C:\WINDOWS\system32> $test1[0] | Get-Member


   TypeName: PSWindowsUpdate.WindowsUpdate

Name                      MemberType   Definition
----                      ----------   ----------
CreateObjRef              Method       System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Equals                    Method       bool Equals(System.Object obj)
GetHashCode               Method       int GetHashCode()
GetLifetimeService        Method       System.Object GetLifetimeService()
GetType                   Method       type GetType()
InitializeLifetimeService Method       System.Object InitializeLifetimeService()
ToString                  Method       string ToString()
ComputerName              NoteProperty string ComputerName=myhostname
KB                        NoteProperty string KB=KBxxxxxx1
Size                      NoteProperty string Size=66MB
Status                    NoteProperty string Status=-------

The other item, $test1[1] has the same features.

They are not a Property, but a NoteProperty.

Edit: I could extract their values with this solution, but I wonder if there’s a simpler way to do this.


PS C:\Windows\System32> $test1[0] | foreach { $_.psobject.properties | foreach { Write-Host $_.value } }
66MB
-------
myhostname
KBxxxxxx1

well color me confused. Based on the output from Get-Member you should absolutely be able to call those properties by name with that “dot” method you were using.
I might have to install the module and take a look.

1 Like

I’m still confused.


Installed the module on a Windows 11 VM (running Atlas OS). The object claims to be a ComObject, but when I do Get-Member it shows the same NoteProperties you have.
But, I can access the properties
image
I’ll have to try it on a different Windows machine that’s not Atlas OS.

1 Like

Thanks for all your attempts. Don’t worry, don’t install it again. Probably the behaviour of Get-WindowsUpdate is quite random and it depends on the system or the shell itself (in PowerShell 7.4 I get an empty object, which however has non empty fields Name and KB!). I am on Windows 11 on a physical machine.

Even if it’s not optimal, I can still access those properties with the workaround in the previous post.

1 Like

Com objects are notorious for this type of thing. Simply surround them with subexpression

$($test1[0]).ComputerName

or i think even

$($test1)[0].ComputerName

Another thing that can help is to just run it through the pipeline

$test1 = Get-WindowsUpdate | ForEach-Object {$_}

I have to do this frequently when using the outlook com object.

1 Like

Thanks for mentioning this. I tried, but in my case (Windows 11, Windows PowerShell 5.1) it didn’t work.

In both cases

$($test1[0]).ComputerName
$($test1)[0].ComputerName

I can make autocompletion with tab for ComputerName, but then the field is printed as empty.

The same if $test1 is obtained as $test1 = Get-WindowsUpdate | ForEach-Object {$_}.

That is really, really odd.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.