Home > Software engineering >  powershell : store a specific line in a variable and reuse it
powershell : store a specific line in a variable and reuse it

Time:01-20

i would like to know how can i store in a variable the output of an get-wmiobject specific line

eg:

PS C:\Users\gaga> get-wmiobject Win32_Product -Filter "Name like '%team%'" | Sort-Object -Property Name |Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

IdentifyingNumber                      Name                         LocalPackage                   
-----------------                      ----                         ------------                   
{ABC12345-1234-1234-8936-123456789ABC} Teams Machine-Wide Installer C:\Windows\Installer\523ff8.msi

--> (totaly fictive numbers)

i would like to store this : $var1={ABC12345-1234-1234-8936-123456789ABC}

to reuse it inside this command :

Start-Process "msiexec.exe" -ArgumentList "/x $var1 /quiet /n" -Wait

thanks you.

CodePudding user response:

The problem here is the Format-Table cmdlet. This "cosmetic" type cmdlet sends output (only) to the console, can't re-use it (output). Lose that in the pipeline and it will work just fine, like so:

$obj = get-wmiobject Win32_Product -Filter "Name like '%team%'" | Sort-Object -Property Name

$var1 = $obj.IdentifyingNumber

CodePudding user response:

this did the trick :

$guidapp = get-wmiobject Win32_Product -Filter "Name like '%teamviewer%'" | Select-Object -ExpandProperty IdentifyingNumber

Start-Process "msiexec.exe" -ArgumentList "/x $guidapp /quiet /n" -Wait

thx :)

  •  Tags:  
  • Related