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

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

Copying variables to local text file from multiple ssh awk output

powershell multiple column output

ForEach with Multiple Variables - Powershell

Returning multiple variables from within method in powershell class

load output to variables directly powershell

Output PowerShell variables to a text file

Python: color output with multiple variables

PowerShell variables from csv

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

Assigning output from awk to variables

JS expression to calculate and set an input value based on multiple variables from previous response output

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

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

How to enter a sum derived from awk in the middle of text output

Elm output port is not working with a signal derived from StartApp

Get data from elements within multiple XML files for output to another, single XML file using Powershell

How to store curl output in multiple variables in Bash

Optimize code for output multiple int variables into String

In ash how to set multiple line output to variables

Reading delimited output into multiple Bash variables

Set multiple variables to different fields of awk output

TOP Ranking

HotTag

Archive