0

I have what is probably a simple question.

My goal is to turn the results of the get-appvclientpackage cmdlet into a list from which I can take the product names and execute commands with each of the results as a different variable.

I have below my simple code:

    $installedpackages = get-appvclientpackage
    $displayedpackages = 
    $installedpackages | Format-List -property Name
    Foreach ($package in $displayedpackages)

That is about as far as I get. I can get it to display the list as I require but I need the results of the list to be assigned to variables that I can use to execute with strings later on in my full script.

Been through switch statements and I can get around the same result but never able to get the results assigned to variables. Any tips would be awesome!

  • 1
    Usually there is no need to programmatically create a list of variables. You can use a loop to iterate over an array of elements in PowerShell. What's your actual goal? So you could iterate over `$installedpackages` and execute actions on all individual elements. – Olaf May 05 '22 at 19:44
  • Olaf, correct. I want to be able to list the installed packages and let's say there are 4 of them on a client machine. I want to be able to execute actions on 1 and 3 because I need them removed. I would be able to tie $1 and $3 to install files located on the client machine as needed and do other actions that might refer those $1 and $3 variables. – Treeman May 05 '22 at 19:57
  • As an aside: `Format-*` cmdlets output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's for-display output-formatting system. In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_ - see [this answer](https://stackoverflow.com/a/55174715/45375) for more information. – mklement0 May 05 '22 at 20:13
  • Perhaps all you need is `$displayedpackages = $installedpackages | Select-Object -ExpandProperty Name` or, more simply, `$displayedpackages = $installedpackages.Name` – mklement0 May 05 '22 at 20:15
  • @tfree-man How do you decide if it's 1 and 3? Why not 2 and 4? If I got it right you simply need a condition inside of a loop. Or filter the list for the needed ones and run the actions on all of the members of the remaining list. ;-) – Olaf May 05 '22 at 20:45
  • 1
    @mklement0 thank you for the info in regards to the Format-*. Was not aware, that helps. Will probably use the .name format you listed instead. – Treeman May 05 '22 at 21:13
  • @Olaf kind of. I need to be able to give whoever runs the full script the ability choose which package they want to be able run the actions on. The Foreach statement in theory should list the 4 options and I should be able to choose which of the 4 items I want run actions against. I may not be able to explain it better until I have written more out. – Treeman May 05 '22 at 21:19
  • 1
    @tfree-man If it should be an interactive process you may take a look at the cmdlet [Out-GridView](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-gridview?view=powershell-7.2). With its parameter `-OutputMode` set to `Multiple` you can select the desired items with the mouse and confirm it with a click of a button. The selected items will be outputted. ;-) – Olaf May 05 '22 at 21:26
  • You can pipe the command into `Get-Member` to see more informtion about the output of a command. `Get-AppVClientPackage | Get-Member` would give you more helpful info. – another victim of the mouse May 05 '22 at 21:40

0 Answers0