Home > Software engineering >  POWERSHELL : How to get the last characters (can be 3 or 10) of Powershell output (WINGET App Versio
POWERSHELL : How to get the last characters (can be 3 or 10) of Powershell output (WINGET App Versio

Time:09-29

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.

CodePudding user response:

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
}

CodePudding user response:

Here's a parsing attempt. Bizarrely, the first blank line has backspaces and a dash in it. Return is like continue for foreach-object.

# invoke-winget.ps1

$first = 1

$out = "   `b-`b ", 
'Name                     Id                   Version',
'------------------------------------------------------',
'x264                     VideoLAN.x264        3098',
'VLC media player nightly VideoLAN.VLC.Nightly 4.0.0',
'VLC media player         VideoLAN.VLC         3.0.17.4'

$out = winget $args

$out | ForEach-Object {
    if ($_ -match '^   [\b]-[\b] |^---') { 
        return # skip first blank line or dashes line
    } 
    elseif ($first -eq 1) {
        $NamePos = $_.IndexOf("Name")
        $IdPos = $_.IndexOf("Id")
        $VersionPos = $_.IndexOf("Version")
        $first = 0
    }
    else {
        $name = $_.substring($NamePos,$IdPos-$NamePos).Trim()
        $id = $_.substring($IdPos,$VersionPos-$IdPos).Trim()
        $version = $_.substring($VersionPos,$_.length-$VersionPos).Trim() ) -replace 'Tag: .*'
        [pscustomobject]@{Name = $name; Id = $Id; Version = $version}
    }
}

Examples (3098 can't be converted to [version]):

.\invoke-winget search VideoLAN --source winget


Name                     Id                   Version
----                     --                   -------
x264                     VideoLAN.x264        3098
VLC media player nightly VideoLAN.VLC.Nightly 4.0.0
VLC media player         VideoLAN.VLC         3.0.17.4


# case sensitive
.\invoke-winget search --exact VideoLAN.VLC --source winget

Name             Id           Version
----             --           -------
VLC media player VideoLAN.VLC 3.0.17.4


.\invoke-winget search --exact VideoLAN.VLC --source winget | % version

3.0.17.4
  • Related