Skip to content

Instantly share code, notes, and snippets.

@ctsstc
Last active July 17, 2025 17:39
Show Gist options
  • Save ctsstc/59642b271ce464a54f500fa46af385be to your computer and use it in GitHub Desktop.
Save ctsstc/59642b271ce464a54f500fa46af385be to your computer and use it in GitHub Desktop.
Update Qbittorrent Forward Port from PIA on Network Change

Update qBittorent Forward Port from PIA on Network Change

This updates qBitorrent's port to match PIA's forwarded port whenever PIA connects.

Installation

  • Save update-qbittorent-port-from-pia.ps1 somewhere
  • Open Windows Task Scheduler
  • Create a new folder under Task Scheduler Library
  • Import Task...
  • Actions tab: make sure to point the path to update-qbittorent-port-from-pia.ps1

Note: you may need to change the path to piactl.exe in the script.

Qbittorrent Configuration

  • Goto Tools > Options > WebUI
  • Enable: Web User Interface
  • Enable: Bypass authentication for clients on localhost
  • Make sure the port is set to 9001 or change it to match in the script

Note: this only works when qBittorent is open.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2022-07-04T02:03:04.0327046</Date>
<Author>Coder https://github.com/ctsstc</Author>
<Description>Run script that updates Qbittorrent's forward port from PIA's forwarded port when
the network changes and a forwarded port is available.</Description>
<URI>\CTS_AE\Update Qbittorrent Forward Port from PIA</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0"
Path="Microsoft-Windows-NetworkProfile/Operational"&gt;&lt;Select
Path="Microsoft-Windows-NetworkProfile/Operational"&gt;*[System[Provider[@Name='Microsoft-Windows-NetworkProfile']
and EventID=10000]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-373059960-1286789452-1330396964-1002</UserId>
<LogonType>S4U</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-ExecutionPolicy Bypass -File
"C:\Users\coder\Desktop\update-qbittorent-port-from-pia.ps1"</Arguments>
</Exec>
</Actions>
</Task>
# Run piactl.exe and get the current open port
function Get-PiaPortForwarded {
# $output = cmd /c "C:\Program Files\Private Internet Access\piactl.exe" $args 2`>`&1
# $output = Get-ProcessOutput -FileName "cmd.exe" -Args "/c ping localhost"
# $output = [string] (cmd /c "C:\Program Files\Private Internet Access\piactl.exe" $args 2>&1)
$output = (cmd /c 'C:\Program Files\Private Internet Access\piactl.exe' get portforward) | Out-String
$output.trim()
}
# Check if the process qbittorrent.exe is running
$process = Get-Process -Name "qbittorrent" -ErrorAction SilentlyContinue
if ($process) {
# We could just kill it and restart it with the correct port
# Instead we'll utilize the the Web API to set the port
# if it is running, kill it
# $process | Stop-Process -Force
# & 'C:\Program Files\qBittorrent\qbittorrent.exe' --torrenting-port=3456
}
else {
# Quit if it's not running
Write-Host "qBittorrent is not running"
exit
}
# if $output is "Inactive" or "Attempting" then wait 2 seconds and try again, stop trying after 10 times and exit
$output = Get-PiaPortForwarded
if ($output -notmatch "^\d+$") {
$i = 0
do {
Write-Host "Port forwarding is '$output' [Trying Again $i/10]"
Start-Sleep -Seconds 2
$output = Get-PiaPortForwarded
$i++
} while (($output -notmatch "^\d+$") -and $i -lt 10)
}
# if $output is not a number then exit
if ($output -notmatch "^\d+$") {
Write-Host "Did not find port forwarding for: '$output'"
exit
}
# if $output is a number then run qBittorrent's API to set the listening port
Write-Host "Found Forwarded Port: $output"
Invoke-WebRequest -Uri "http://localhost:9001/api/v2/app/setPreferences" `
-Method "POST" `
-Body "json=%7B%22listen_port%22%3A%20$output%7D"
Write-Host "Port Forwarding Finished"
# Write-Host "Press any key to continue..."
# $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment