Back to blog

How to Run Python Script in PowerShell

How to Run Python Script in PowerShell
author avatar

Augustas Pelakauskas

2025-03-203 min read
Share

To run Python scripts, most commonly, you can use:

  • Command line interface

  • Integrated development environment (IDE)

  • Interactive mode

The following guide is for using the PowerShell command line interface as a Python interpreter.

Running Python commands from PowerShell is a common task that combines Python's versatility with PowerShell's powerful scripting capabilities. This tutorial covers:

  1. Installing Python

  2. Creating a simple Python script

  3. Running the Python script from PowerShell

What is PowerShell?

Windows PowerShell is a command-line shell and full-featured scripting language developed by Microsoft for task automation and configuration management.

PowerShell is available on Windows, macOS, and Linux, while PowerShell Core is open-source and cross-platform.

On Windows, compared to the default Command Prompt (cmd), which offers basic file and system operations through simple text commands, PowerShell is a more powerful tool. It works with objects rather than text streams, letting you manipulate structured data.

PowerShell can handle complex automation, remote system management, and integration with other Microsoft technologies.

PowerShell on Windows

PowerShell on Windows

Tutorial on how to run a Python script in PowerShell

1. Installing Python

To run Python scripts, you first need to have Python installed on your system.

  • Download the installer: Visit the official Python website at python.org and download the latest version.

  • Run the Installer: Execute the downloaded installer. During installation, check Add Python to PATH. It’s crucial as it allows you to run Python from any command line interface, including PowerShell. Also, ensure the pip is included in the installation components.

  • Verify the installation: After installation, open PowerShell and type:

python --version
pip --version

If Python is installed correctly, this command will display the Python version.

2. Creating a simple Python script

Let's create a Python script that sends a request to retrieve your IP address.

The script uses the requests library to send an HTTP GET request to https://ip.oxylabs.io and prints your public IP address.

Open your preferred text editor and install the requests library:

pip install requests

Write the code:

# ip_check.py
import requests
def get_public_ip():
    try:
        response = requests.get('https://ip.oxylabs.io')
        if response.status_code == 200:
            print(f"Your IP address: {response.text.strip()}")
        else:
            print("Failed to retrieve IP address.")
    except Exception as e:
        print(f"An error occurred: {e}")
if __name__ == "__main__":
    get_public_ip()

The Python script checks if the request is successful (status code 200) and prints an error message if the request fails.

The script also catches and handles any exceptions that occur during the request and prints the error message.

Save the script file as ip_check.py in a directory of your choice.

3. Running the Python script from PowerShell

Now, let's execute the Python file using PowerShell.

In PowerShell, specify the directory where ip_check.py is located:

cd path\to\your\script

Run the script:

python ip_check.py

You should see a similar output:

Your public IP address: 124.45.65.80

Using proxies for sending HTTP requests

If you need to send HTTP requests to get content (not only a response) from a web page, the process above requires an addition – proxies.

Proxies serve as intermediaries between your scraping tool and the target website, masking your actual IP address. Such anonymity is crucial for large-scale scraping projects as websites often implement IP-based limiting or blocking mechanisms.

By rotating through multiple proxies, you can distribute requests across different IP addresses, making your scraping activities appear as regular user traffic from diverse locations.

Using proxies enables you to bypass geographical restrictions, accessing region-specific content that might otherwise be unavailable. This capability is invaluable for market research, pricing analysis, or content aggregation.

Things to note when choosing proxies

  • Residential proxies are the most popular proxy type. They are suitable for most scenarios, while datacenter proxies are faster and more affordable but prone to detection by target websites.

  • You can integrate proxies on Windows or any other OS through your system settings or from the web scraper’s side when sending requests for more control.

  • Free proxies for smaller tasks are a good idea. However, maintaining a quality proxy pool is expensive. Therefore, the wisest approach is to look for a paid proxy service provider that also offers some free proxies.

Wrap up

By following these steps, you can run Python scripts with PowerShell. This integration lets you leverage Python commands within the Windows environment, enhancing your scripting and automation tasks.

Web scraping with PowerShell is feasible, but Python web scraping using, for example, an IDE is way more convenient. Anyway, among many Python use cases, data manipulation is its most prominent feature.

About the author

author avatar

Augustas Pelakauskas

Senior Copywriter

Augustas Pelakauskas is a Senior Copywriter at Oxylabs. Coming from an artistic background, he is deeply invested in various creative ventures - the most recent one being writing. After testing his abilities in the field of freelance journalism, he transitioned to tech content creation. When at ease, he enjoys sunny outdoors and active recreation. As it turns out, his bicycle is his fourth best friend.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I'm interested