powershell output multiple variables derived from processlist

IGGt

I have the below bit of code, it is designed to simply ask for a Windows ProcessName, and then return a list of all instances of that process that are running.

$processName = Read-Host 'Please Enter the Process Name, as shown in Task Manager (not inc *32)'
if (!$processName) 
{ 
    "No Process Given"
}
else
{
    "You Entered "+$processName

    $filter = "name like '%"+$processName+"'"
    $result = Get-WmiObject win32_process -Filter $filter | select CommandLine

    $counter=1
    foreach($process in $result )
        {
            write-host "$counter) $process"
            $counter++
        }    
}

It works fine up until the point where it outputs the list.

If I do

echo $process

then I get what I am after e.g.

"C:\folder\AppName.exe"
"C:\folder\AppName.exe instance1"

If however I try to concatenate the $counter in front of it I get:

1) @{CommandLine="C:\folder\AppName.exe" }      
2) @{CommandLine="C:\folder\AppName.exe instance1" }

I've tried write-host, write-output, echo, various combinations of "", +, but I can't get rid of the @{CommandLine= xxx } when I try to combine it with another variable

Is there a way to get what I am after? e.g.:

1) "C:\folder\AppName.exe"
2) "C:\folder\AppName.exe instance1"
Loïc MICHEL

try write-host "$counter) $($process.commandline)" OR modify your selection : $result = Get-WmiObject win32_process -Filter $filter | select -expandproperty CommandLine

explanation : without expandproperty you get a psobject with expandproperty you have a string

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return multiple output variables from postgres function

Selecting multiple strings from the output of command in powershell

Powershell reading multiple variables from text file

multiple PowerShell variables come up blank when in a line of output

defining derived variables from for loops

Format output from Variables Table-View in PowerShell

powershell multiple column output

ForEach with Multiple Variables - Powershell

Copying variables to local text file from multiple ssh awk output

Prevent output from writing multiple times in Powershell script

How to Combining PowerShell Output from multiple server to Single csv

Returning multiple variables from within method in powershell class

Powershell create multiple environment variables from .env file

load output to variables directly powershell

Output PowerShell variables to a text file

Terraform Output to set variables in PowerShell

PowerShell variables from csv

Python: color output with multiple variables

How to logical OR multiple variables in Powershell

Unable to use output variables from a PowerShell script in the Override Parameters of a Resource Group deployment task in Azure DevOPS

How to see full query from SHOW PROCESSLIST

Cannot set powershell output variables in Devops step

How do I implement output variables in powershell?

echo variables in powershell using Write-Output

Suppressing unwanted output in powershell when declaring variables

Adding Variables to Powershell Get-Acl output

show Powershell variables and foreach output in email

How to filter the output from the output of this powershell command?

Assigning output from awk to variables