Avatar of Attila Mosonyi
Attila Mosonyi
 asked on

How to run a poweshell command from a batch file

I would like to run a powershell command:
Get-Process "auto ftp manager" | Where StartTime -lt (Get-Date).AddMinutes(-30) | Stop-Process -Force
every 12 hours on a server automatically.
I was thinking to use a batch file and windows scheduler to run it.
How do I make the powershell command run from a batch file?
Is there a better solution to achieving this task, i.e. running the ps command automatically every 12 hours?
PowershellFTPWindows OS

Avatar of undefined
Last Comment
Attila Mosonyi

8/22/2022 - Mon
Ryan Chong

you can call a powershell script via task scheduler directly without the use of batch file.
Attila Mosonyi

ASKER
Hi Ryan, I need help with creating the powershell script for:
Get-Process "auto ftp manager" | Where StartTime -lt (Get-Date).AddMinutes(-30) | Stop-Process -Force
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Attila Mosonyi

ASKER
As I mentioned before, I don't have any experience/knowledge with powershell, so please someone help me with creating a script (.ps1) for the following:
Get-Process "auto ftp manager" | Where StartTime -lt (Get-Date).AddMinutes(-30) | Stop-Process -Force
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Attila Mosonyi

ASKER
Putting the commandlet simply into a *.ps1 doesn't do the job.
I tested it by simply running it manually. It only completes the job, when the powershell is running in elevated mode, i.e. as administrator. So how do I put this in the *.ps1?
oBdA

Again the same as in batch/cmd.exe (or any other program that requires elevation); you start a PS console by right-clicking the shortcut and select "Run as Administrator", and run the script from there.
When scheduling the task, you have a checkbox option "Run with highest privileges", or you just use the System account to run the task under.
Attila Mosonyi

ASKER
Thank you Ryan Chong and oBdA very much for your assistance :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.