Search code examples
restpowershelltrim

trim results from invoke-restmehod


I'm running a Invoke-RestMethod to an api and do a select-object for the result.

$result=Invoke-RestMethod -Uri $Url -Method Get $result|Select-Object ID,User,Descripton

result is like this:

ID User Description                                                  
-- ---- -----------
1 1234  HP EliteBook 840 G3 - 14” - Core i7 6500U - 8 GB RAM - 256 GB SSD
2 3251  HP EliteBook 840 G3 - 14” - Core i7 6500U - 8 GB RAM - 256 GB SSD
3 4323  HP EliteBook 840 G3 - 14” - Core i7 6500U - 8 GB RAM - 256 GB SSD

is there a way to trim the Description value on the fly so it only says the model? like this

ID User Description                                                  
-- ---- -----------
1 1234  HP EliteBook 840 G3
2 3251  HP EliteBook 840 G3
3 4323  HP EliteBook 840 G3

Solution

  • Looks like you could split on - then take the first entry in the array. As Mark pointed out you should put this in a calculated property.

    $result=Invoke-RestMethod -Uri $Url -Method Get $result | 
        Select-Object ID,User,@{name="Descripton";Expression=($_.Description.split('-')[0]).trim()}