how to get string from powershell's 2 line output

283 views Asked by At

I need to get a string from powershell output. I need to get package name
i.e prompt-toolkit and use it in the pipe below

> pip list --outdated
Package        Version Latest Type
-------------- ------- ------ -----
prompt-toolkit 1.0.15  2.0.3  wheel

pip list --outdated | Where-Object { $exclude -notcontains $_ } | ForEach { pip install -U $_.split(" ")[0] }

UPDATE.
The modified working script

    $(
    $exclude = 'virtualenv', 'prompt-toolkit'
    pip list --outdated --format=freeze  | ForEach{ $_.split("=")[0]} | Where-Object { $exclude -notcontains $_ } | ForEach { pip install -U $_ }                                    
    ) *>&1 >> Python_Modules_Updates_Log.txt
2

There are 2 answers

1
Larry Song On BEST ANSWER

simply try

pip list --outdated | select-object -skip 2 | Where-Object { $exclude -notcontains $_ } | ForEach { pip install -U $_.split(" ")[0] }
0
Paxz On

To get the Packagenames of the list, you can use the -ExpandProperty feature of Select-Object and pipe it to the install command:

pip list --outdated | Select-Object -ExpandProperty Package | Where-Object { $exclude -notcontains $_ } | ForEach { pip install -U $_}