PowerShell: Get OU of a User & Get All Users In an OU

function getUserOu($username){

    if(!(get-module activedirectory -ea SilentlyContinue)){
        $osType=switch ((Get-CimInstance -ClassName Win32_OperatingSystem).ProductType){
            1 {'client'}
            2 {'domaincontroller'}
            3 {'memberserver'}
            }
        $windowsVersion=[System.Environment]::OSVersion.Version
        $releaseId=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
        $windowsReleaseName=(Get-WmiObject -class Win32_OperatingSystem).Caption
        write-host "$windowsReleaseName with release ID $releaseId detected."
        if($osType -ne 'client'){
            # source: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
            if($windowsVersion -ge [version]'6.2'){
                # If Windows Server 2012 or Newer            
                Install-WindowsFeature RSAT-AD-PowerShell
            }elseif($windowsVersion -ge [version]'6.1'){
                # The Active Directory module for Windows PowerShell first appeared on Windows Server 2008 R2.
                # It is automatically installed on the Windows Server along with ADDS role
                Import-Module ServerManager
                Add-WindowsFeature -Name "RSAT-AD-PowerShell" –IncludeAllSubFeature
            }else{
                write-warning 'This legacy machine requires a manual install of Administrative Tools pack (Adminpak.msi).'
            }
        }elseif($windowsVersion -ge [version]'6.1'){
            # Accounting for Client OS variants
            if($releaseId -ge 1809){
                Add-WindowsCapability -Online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
            }else{
                Get-WindowsCapability -Name Rsat.ActiveDirectory* –Online|Add-WindowsCapability –Online
            }            
        }else{
            write-warning 'This legacy machine requires a manual install of Administrative Tools pack (Adminpak.msi).'
        }
        if (import-module activedirectory -ea SilentlyContinue){
            return $true
        }else{
            return $false
        }
    }

    $filter="$username*"
    $userObject=get-aduser -f 'SamAccountName -like $filter'
    return $userObject| Select-Object -Property @{
            Name='OU';
            Expression={$_.DistinguishedName.Split(',')[1].replace('OU=','')}
        },DistinguishedName|Format-Table -autosize
}
function getObjectsInOu($ouName){
    
    if(!(get-module activedirectory -ea SilentlyContinue)){
        $osType=switch ((Get-CimInstance -ClassName Win32_OperatingSystem).ProductType){
            1 {'client'}
            2 {'domaincontroller'}
            3 {'memberserver'}
            }
        $windowsVersion=[System.Environment]::OSVersion.Version
        $releaseId=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
        $windowsReleaseName=(Get-WmiObject -class Win32_OperatingSystem).Caption
        write-host "$windowsReleaseName with release ID $releaseId detected."
        if($osType -ne 'client'){
            # source: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
            if($windowsVersion -ge [version]'6.2'){
                # If Windows Server 2012 or Newer            
                Install-WindowsFeature RSAT-AD-PowerShell
            }elseif($windowsVersion -ge [version]'6.1'){
                # The Active Directory module for Windows PowerShell first appeared on Windows Server 2008 R2.
                # It is automatically installed on the Windows Server along with ADDS role
                Import-Module ServerManager
                Add-WindowsFeature -Name "RSAT-AD-PowerShell" –IncludeAllSubFeature
            }else{
                write-warning 'This legacy machine requires a manual install of Administrative Tools pack (Adminpak.msi).'
            }
        }elseif($windowsVersion -ge [version]'6.1'){
            # Accounting for Client OS variants
            if($releaseId -ge 1809){
                Add-WindowsCapability -Online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
            }else{
                Get-WindowsCapability -Name Rsat.ActiveDirectory* –Online|Add-WindowsCapability –Online
            }            
        }else{
            write-warning 'This legacy machine requires a manual install of Administrative Tools pack (Adminpak.msi).'
        }
        if (import-module activedirectory -ea SilentlyContinue){
            return $true
        }else{
            return $false
        }
    }
    
    function getUsersInOu($ouName){
        $filter="$ouName*"
        $ouPaths=(Get-ADOrganizationalUnit -Filter 'Name -like $filter').DistinguishedName
        return $ouPaths|%{Get-ADUser -Filter * -SearchBase $_}| Select-Object Name,@{Name='Principle';e={$_.UserPrincipalName}},DistinguishedName
    }
    function getComputersInOu($ouName){
        $filter="$ouName*"
        $ouPaths=(Get-ADOrganizationalUnit -Filter 'Name -like $filter').DistinguishedName
        return $ouPaths|%{Get-ADComputer -Filter * -SearchBase $_}| Select-Object Name,@{Name='Principle';e={$_.SamAccountName}},DistinguishedName
    }

    $users=getUsersInOu $ouName
    $computers=getComputersInOu $ouName
    $result=$users+$computers
    return $result
}

#getObjectsInOu 'KungFu Fighters' | ft -autosize

Leave a Reply

Your email address will not be published. Required fields are marked *