Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a python script via Powershell script

I have a python script which I can run via PowerShell using the following code:

cd User\PythonScripts
python TestFile.py

Now I want to run these simple commands via a PowerShell script (notepad file and saving it as a ps1 file).

I have googled a lot but I cannot find an answer, but I think it should be something like this:

$path = 'C:\User\PythonScripts'
$file = 'TestFile.py

I think I still miss the reference to python (so it knows which program he needs). How do I need to do this?

like image 315
Oamriotn Avatar asked Apr 22 '26 09:04

Oamriotn


1 Answers

I think the question is you want to run the python script using powershell .

I think below code will do for you

$path = 'C:\User\PythonScripts'
$file = 'TestFile.py'

$cmd = $path+"\\"+$file  # This line of code will create the concatenate the path and file
Start-Process $cmd  # This line will execute the cmd 

Save the above code as .ps1 and run the that powershell file

like image 185
Sreevathsabr Avatar answered Apr 25 '26 00:04

Sreevathsabr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!