We help IT Professionals succeed at work.

How to run a poweshell command from a batch file

Attila Mosonyi
on
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?
Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
you can call a powershell script via task scheduler directly without the use of batch file.
Attila MosonyiIT Support Specialist

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Just schedule the PowerShell script; you just can't schedule the script file directly like a batch, so you need the following:
Program to start: powershell.exe
Arguments: -ExecutionPolicy RemoteSigned -File "P:\ath\To\YourScript.ps1"
Attila MosonyiIT Support Specialist

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Same as in batch: you paste the line into Notepad, save it somewhere as StopFtpManager.ps1 or Whatever.ps1, and schedule it from this location as described above.
Attila MosonyiIT Support Specialist

Author

Commented:
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?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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 MosonyiIT Support Specialist

Author

Commented:
Thank you Ryan Chong and oBdA very much for your assistance :)