Close Grecefully Application With Power Shell

I have an application that has not Exit button but just the window UI "X" (hard to believe) and Im trying to close this application gracefully from a button the application provides by the  use  of Shell, this button has two fields to run a Shell; Command and Command Arguments.

I'm trying to run a PowerShell that when I run it on the PowerShell window work fine;

PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }
It closes the application and it ask if I need to save any changes when appropriate when run from Powershell console.

How can I run this Powershell from the button the application provides?

Can it be run from with in the same application I'm trying to close?

I try to save the file with a .PS1 extension and call it from the Shell button but can not make it work, what is the syntax to do this or how can I run the Powershell line from the button?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Question not clear. Can you explain again please.
Akire4Author Commented:
I need to use a Shell script to close and application, this application has a costume button that can run a Shell, but I don't want not close it by brute force I need to ask User if any changes need to be save. The application Form has a button that has to fields in its properties  for the Shell, field; command and filed; arguments. How can I run a Shell to close it properly?

When I use this Powershell script from the console;

PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }

 it closes the application properly the way I want, so how can I embed this line of script in to my button? or any other way to mimic what this line of script does from the Shell button i have in my application?
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
If you have the object of the form:

$form = New-Object System.Windows.Forms.Form

Open in new window

just use close method on the object.


Open in new window

If you have the code, attach here and someone will help.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Akire4Author Commented:
I don't have access to the IDE, its just a Form that you build by dropping Objects  with proprieties that have some of them available for you to costume the Form. If I use I assume the line of script you are contributing it will work on the IDE but all the access I have is the control  properties to run a Shell (Command and Command Arguments) when I run your scrip lines I get an error; "Error executing shell command! [$form=New-Object System.Windows.Forms.Form] Arguments [$form.close()]
Akire4Author Commented:
Abdul, when I run this on the Powershell command window it work as I want;
PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }

How can I wrap this to run it from the application buttons that accepts Shell Command and Shell Arguments? I try to make a file with the script line and and save it as .PS1 and call it from this button but it fails. What is the easier way to call it from the application?
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
can you send the pictures of how you are running your button? i mean pictures of shell where you want to enter for more clarity.
Akire4Author Commented:
I accomplished my goal by doing this;

On the button Command field (its just a WinForms property that can let you run a Shell Command) I run a VBS Script;

Set objShell = WScript.CreateObject("WScript.Shell")

Why do I had to do this? so I can hide the .bat window when running

Batch file=CloseApp.bat(same name as the PowerShell file)
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -executionPolicy bypass %~dp0%~n0.PS1

The reason for this is that since I can not run the PowerShell from my Button Command but I can run a Batch script  file, when I run the Batch and rename the PowerShell file name the same as the batch file when the Batch file runs it runs my PowerShell script since they both are in the same Folder directory.

PowerShell Script=CloseApp.PS1(same file name as the batch file name)

Get-Process MyApp | ForEach-Object {$_.CloseMainWindow() | Out-Null }

OK so I accomplished my  goal BUT how can I use just one either a VBS or BAT script to do what I accomplished with the 3 scripts; Close my application gracefully by running a command script window in a hidden window?
Akire4Author Commented:
I've requested that this question be deleted for the following reason:

Its been open since 9/26
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.