Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell Script to get Ubiquiti Unifi Sites, Devices and Active Clients. Associated blogpost
# Unifi Controller Login Base URI
$uController = 'yourControllerIP' # e.g ''
# Identifier of the site in UniFi. Set to default for the default site
$uSiteID = "default"
$uUsername = 'adminuser' # yourAdmin UserID
$uPassword = 'yourPassword' # yourAdmin User Password
$uAuthBody = @{"username" = $uUsername; "password" = $uPassword }
$uHeaders = @{"Content-Type" = "application/json" }
# Allow connection with the Unifi Self Signed Cert
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
$uLogin = Invoke-RestMethod -Method Post -Uri "$($uController)/api/login" -Body ($uAuthBody | convertto-json) -Headers $uHeaders -SessionVariable UBNT
if ($uLogin.meta.rc.Equals("ok")) {
Write-Host -ForegroundColor Green "Successfully authenticated to $($uController) as $($uUsername)"
# Get Sites
$uSites = Invoke-RestMethod -Method Get -Uri "$($uController)/api/self/sites" -WebSession $UBNT -Headers $uHeaders
write-host -ForegroundColor Cyan "Sites"
# Get Devices
$uDevices = Invoke-RestMethod -Method Get -Uri "$($uController)/api/s/$($uSiteID)/stat/device" -WebSession $UBNT -Headers $uHeaders
write-host -ForegroundColor cyan "Devices"
# Get Active Clients
$uActiveClients = Invoke-RestMethod -Method Get -Uri "$($uController)/api/s/$($uSiteID)/stat/sta" -WebSession $UBNT -Headers $uHeaders
write-host -ForegroundColor cyan "Active Clients"
else {
Write-Host -ForegroundColor Red "Unsuccessfull in authenticating to $($uController) as $($uUsername)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.