Reputation: 55
I want to check if Outlook, word, Excel, ... are running or not and list them like:
Microsoft Outlook is Running
Microsoft Word is not Running
I have written sth like this:
$ProcessName = "outlook"
if((get-process $ProcessName -ErrorAction SilentlyContinue) -eq $Null)
{ echo "Process is not running" }else{ echo "Process is running" }
and that works for one Processname but do not know how to make more than one and list them.
Upvotes: 1
Views: 14068
Reputation: 41
For those that find this later just an additional note to the above solutions: If you are testing whether Word is running, you need to search for the process "winword" instead of just "word"
@("outlook", "word", "winword") | ForEach-Object {
if(($p=(Get-Process $_ -ErrorAction SilentlyContinue)) -eq $null)
{Write-Host "$_ not running"}
else
{Write-Host "$($p.Description) is running"}
}
yields the result:
Microsoft Outlook is running
word not running
Microsoft Word is running
For any others, open your Task Manager, right click on the process, select Properties and then use the filename portion (without the extension)
Upvotes: 1
Reputation: 62472
You can pass a list of processes in like this:
@("outlook", "word") |
ForEach-Object
{
if((Get-Process $_ -ErrorAction SilentlyContinue) -eq $null)
{Write-Host "$_ not running"}
else
{Write-Host "$_ is running"}
}
NOTE: I've split this across multiple lines for readability.
If you want something a bit more descriptive you can use the Description
property on the process if it exists:
@("outlook", "word") |
ForEach-Object
{
if(($p=(Get-Process $_ -ErrorAction SilentlyContinue)) -eq $null)
{Write-Host "$_ not running"}
else
{Write-Host "$($p.Description) is running"}
}
Note how it captures the result from Get-Process
into $p
and that we have to use the syntax $($p.Description)
to print it in Write-Host
.
Upvotes: 4
Reputation: 1620
There is a thousand ways you could achieve this. I'd go for something like this:
@(
"Word",
"Outlook",
"Excel"
) | Foreach-Object {
if (Get-Process $_ -ErrorAction SilentlyContinue)
{ Write-Output "$_ is running" }
else { Write-Output "$_ is not running" }
}
This is taking an array of whatever applications you want to check, then looping through them checking if they are running and displaying the appropriate output.
Upvotes: 2