Search code examples
powershellwinget

POWERSHELL : How to get the last characters (can be 3 or 10) of Powershell output (WINGET App Version)


I've been grabbing my hair on this one (not much left).

I'm trying get the last characters on a powershell output.

Here's my basic code as example

$VLCVersion = winget search --exact VideoLAN.VLC --source winget

Foreach($line in $VLCVersion){ If($line -like “VLC”) { Write-host $line} }

$line

Example:

  • winget search --exact VideoLAN.VLC --source winget

Result: It gives me the following result

Name Id Version VLC media player VideoLAN.VLC 3.0.17.4

I remove all the lines except the one related to 'VLC' to get to this

VLC media player VideoLAN.VLC 3.0.17.4

How do I get the version number in it's own string

I only want 3.0.17.4 to show.


Solution

  • I don't consider this a robust enough solution but for this particular case, you can accomplish it via simply splitting on white space and then getting the last token. You can then use TryParse(String, Version) to get the correct object:

    $wget    = winget search --exact VideoLAN.VLC --source winget
    $version = $wget[-1] -split '\s'
    
    [ref] $ref = $null
    if([version]::TryParse($version[-1], $ref)) {
        $ref.Value
    }