0

I have some powershell script . Instead totalcmd* you can type another process you want.

$tc = get-process -Name totalcmd* | format-wide -property Name 
echo $tc
if ($tc -eq "Totalcmd64")
{
Stop-Process -Name totalcmd*
}
Start-Sleep 10

It doesn't work, I think, because, my $tc not equal to string "totalcmd". How can I remove unwanted spaces of cmdlet get-process -Name totalcmd* | format-wide -property Name output and compare strings correctly?

1
  • 1
    If you end up with more than one process that starts with "totalcmd", then your "if" statement will never evaluate to true because the array object $tc will never equal a string object "Totalcmd64". Commented Mar 8, 2019 at 12:06

3 Answers 3

1

You do not end up with a string because you pipe to format-wide. These cmdlets are best for representing data on the screen. Instead select the property and use -ExpandProperty to return it as a string:

$tc = get-process -Name totalcmd* | Select-Object -ExpandProperty Name 
echo $tc
...
Sign up to request clarification or add additional context in comments.

Comments

1

You are generally correct that $tc is not equal to "totalcmd" and that is because when you set $tc, you are creating an array (most likely of one element). You can test that by running $tc | get-member to see what kind of object you are working with.

To work with string objects, you could use the Out-String cmdlet as well.

Comments

1

If you want to explicitly stop TotalCmd64 processes why not simply use:

Get-Process -Name TotalCmd64 | Stop-Process

If you want to switch between 64/32bit versions of the program, use a switch statement (untested):

$tc = (Get-Process -Name TotalCmd*).Name
switch ($tc){
    'TotalCmd'   {Get-Process -Name TotalCmd  |Stop-Process;"Start TotalCmd64";Break}
    'TotalCmd64' {Get-Process -Name TotalCmd64|Stop-Process;"Start TotalCmd32";Break}
    default      {"No TotalCmd* processes found"}
}

2 Comments

May be you right. Globally, I need script for restart Totalcmd64.exe to Totalcmd.exe and vice versa.
See extended answer to switch between 64/32bit versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.