Score:0

how to Extract multiple OU name using Powershell's Get-ADOrganizationUnit & Get-ADComputer

in flag

In AD hostnames are created under location OU, inside location OU under computer sub OU. i need a script to extract both OU in excel. i am able to extract one OU details through below scrpit

Get-ADComputer -filter * -Properties ipv4Address, OperatingSystem,DistinguishedName |
    select-object Name, ipv4Address, OperatingSystem,
        @{label='OU';expression={$_.DistinguishedName.Split(',')[1].Split('=')[1]}}

i need help to extract main OU details please help

Score:1
cn flag

If you want all OUs in the path, you can use a regex pattern to extract them.

[regex]$pattern = '(?<=OU=)(.+?)(?=,\w{2}=)'

$searchbase = 'OU=Company,DC=Domain,DC=LOCAL'

$properties = 'Name',
              'ipv4Address',
              'OperatingSystem',
              @{n='OU';e={$pattern.Matches($_.DistinguishedName).Value -join ', '}}

$adparams = @{
    Filter     = '*'
    Searchbase = $searchbase
    Properties = 'ipv4Address',
                 'OperatingSystem'
}

Get-ADComputer @adparams |
    Select-Object $properties

If you're after just the OU above computers, then something like this may work.

[regex]$pattern = '(?<=OU=)(.+?)(?=,\w{2}=)'

$searchbase = 'OU=Company,DC=Domain,DC=LOCAL'

$properties = 'Name',
              'ipv4Address',
              'OperatingSystem',
              @{n='OUs';e={$pattern.Matches($_.DistinguishedName)[1].Value}}

$adparams = @{
    Filter     = '*'
    Searchbase = $searchbase
    Properties = 'ipv4Address',
                 'OperatingSystem'
}

Get-ADComputer @adparams |
    Select-Object $properties 
darshan kumar avatar
in flag
Thanks for the reply. This script worked as expected but i have few more query when i extract the data all the OU details are coming in same column (SERVERS, COMPUTERS, DLF) but i need that is different columns how to split the data can you please help me
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.