Automate Python Scripts in PowerShell with Absolute Paths
Programming
Programming/Python
Tools & Platforms
Tools & Platforms/PowerShell
Published
November 4, 2025
Introduction
This post demonstrates how a simple Python script can be automated with PowerShell on Windows.
As a simple test project, the automation performs a trivial task: creating a timestamped log file every minute.
1. Create the project folder structure
All project files are organized under the directory C:\projects\automate_python_with_powershell_abs. The folder structure can be created automatically by running the following Python script.
This script creates the main project directory and its subfolders (scripts, powershell, logs), reports which ones already exist, and lists the current contents of the base directory.
import os# define the base directory for the projectbase_dir =r"C:\projects\automate_python_with_powershell_abs"# define subfolders for organizationfolders = ["scripts", "powershell", "logs"]# create the folders and report their statusfor folder in folders: path = os.path.join(base_dir, folder)ifnot os.path.exists(path): os.makedirs(path)print(f"created: {path}")else:print(f"already exists: {path}")print("\nproject folder structure is ready.\n")# list the current contents of the base directoryprint("folders:", os.listdir(base_dir))
A simple Python script is used to create a timestamped log file each time it is executed.
The script can be saved as: C:\projects\automate_python_with_powershell_abs\scripts\create_log_file_absolute.py
Each execution of this script generates a new log file in the logs directory, named according to the current timestamp.
A PowerShell script is used to run the Python file automatically every 60 seconds.
It can be saved as: C:\projects\automate_python_with_powershell_abs\powershell\run_automation_minute_absolute.ps1
This PowerShell script continuously executes the Python file, waits 60 seconds, and then repeats the process.
It serves as the automation layer that schedules and triggers the Python script.
# change the current working directory to the project folderSet-Location"C:\projects\automate_python_with_powershell_abs"# run the Python executable, call the log file script and wait 60 seconds before repeatingwhile($true){&"C:\Python312\python.exe""C:\projects\automate_python_with_powershell_abs\scripts\create_log_file_absolute_path.py"Start-Sleep-Seconds 60}
4. Run the automation
The automation can be started from PowerShell using the following command:
Once started, a new log file is created every minute inside the logs folder.
5. Verifying the output files
After the automation had been running for a couple of minutes and was stopped, the logs folder can be checked using the Python code below to view the files that were automatically created.
import osprint("Log files in the 'logs' folder:\n", os.listdir(r"C:\projects\automate_python_with_powershell_abs\logs"))# alternatives:# print(*os.listdir(r"C:\projects\automate_python_with_powershell_abs\logs"), sep="\n")# print(", ".join(os.listdir(r"C:\projects\automate_python_with_powershell_abs\logs")))
6. Stopping the automation
The PowerShell script runs continuously in an infinite loop.
To stop it, several methods can be used depending on how the automation was started:
Manual stop: If the script is running in the same PowerShell window, it can be stopped with Ctrl + C, or by closing the window entirely.
Find and stop by script name: If the automation was started in a new (minimized or hidden) PowerShell window, the background process running the script can be terminated from another PowerShell session using:
# Get a detailed list of all running processes (includes CommandLine info)Get-CimInstance Win32_Process |# Filter to include only those where the command line mentions our PowerShell scriptWhere-Object{$_.CommandLine-like"*run_automation_minute_absolute.ps1*"}|# For each matching process, stop it using its process IDForEach-Object{Stop-Process-Id $_.ProcessId-Force }
7. Cleaning up the project directory (optional)
The entire project directory, including its subfolders, can be removed either from Python or directly from PowerShell. This is useful when resetting or repeating the setup during testing.
Option 1 – Delete with Python
import shutil # provides high-level file and folder operations not available in osif os.path.exists(base_dir): shutil.rmtree(base_dir)print(f"Deleted: {base_dir}")else:print("Directory not found.")
This Python snippet removes the entire folder structure recursively, including all subdirectories and files inside automate_python_with_powershell_abs. The action is permanent and cannot be undone.
Option 2 – Delete with PowerShell
This single PowerShell command performs the same cleanup, removing all files and folders within the project directory silently.
In this example, a simple automation setup was demonstrated using PowerShell and Python.
A separate post will cover the same workflow using relative paths.