Search code examples
regexwindowspowershellwindows-update

Powershell - Extract KB name from cab file


I'm trying to extrapolate the KB name from a cab file since it needs to be used in a script to validate the patch installation.

File names can be different, however the pattern seems always like KB1234567 (KB + 7 digits).

I've used the following to figure it out, however I was looking for a better way in case this changes:

PS C:\Users\user> $name
IE11-Windows6.1-KB4089187-x64.cab
PS C:\Users\user> $name.Substring($name.indexof("KB")).TrimEnd("-x64.cab")
KB4089187

Solution

  • As @JeffZeitlin says in his comment, you could use some other technique if the name follows a consistent pattern. However, if the full name is not standard, but contains KBxxxxxxx somewhere, you can use Select-String like this to find it:

    $name = "IE11-Windows6.1-KB4089187-x64.cab"
    
    $namesplit = (Select-String -InputObject $name -Pattern "KB\d{7}").Matches[0].Value