# Import Active Directory module (if not already loaded)

Import-Module ActiveDirectory


# Function to check if an OU contains any computers

function HasAnyComputers($ouPath) {

  # Get all child objects of the OU

  $childObjects = Get-ADObject -Filter * -SearchBase $ouPath -Properties ObjectClass


  # Check if any child objects are computers

  return $childObjects | Where-Object { $_.ObjectClass -eq "Computer" }

}


# Find all OUs under the domain root (DC=cyp,DC=local)

$ous = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=cyp,DC=local" -SearchScope Subtree


# Build the output string

$output = ""

foreach ($ou in $ous) {

  if (HasAnyComputers $ou.DistinguishedName) {

    $output += $ou.DistinguishedName + "`n"

  }

}


# Choose output format (comment out unwanted option)

# # Output to a text file

# $outputPath = "C:\test2\output.txt"

# Out-File -FilePath $outputPath -InputObject $output


# # Output to an HTML file

$outputPath = "C:\test2\output.html"

$htmlContent = "<pre>$output</pre>"

Out-File -FilePath $outputPath -InputObject $htmlContent -Encoding UTF8


# Note: Replace "C:\path\to\output.txt" and "C:\path\to\output.html" with your desired file paths.


Write-Host "Output saved to: $outputPath"