Score:0

How can i change parent container reversed in powershell?

in flag

I have the need of extract all users of AD with all information about them adding a field with "parent container" field reversed

For example:

Actual parent container field: "OU=Users,OU=Area,DC=Company,DC=com"

Needed format parent container field: Company.com\Area\Users

And here the code i have right now

Get-ADUser -Filter * -Properties * | select *,@{l='Parent';e={([adsi]"LDAP://$($_.DistinguishedName)").Parent}} | export-csv \\server\folder\usersADps.csv

Thanks

br flag
[1] replace the final `,dc=com` with `.com` [2] split on `,dc=` [3] split the 1st part on the `ou=` [4] trim away the unwanted `,` [5] use `[array]::reverse` to reverse things [6] join them with `-join '\'`
my flag
Why not just use the canonicalname property?
Score:0
in flag

I wrote a PowerShell module a while back that encapsulates the functionality of the NameTranslate and Pathname COM objects for easy use within PowerShell:

https://github.com/Bill-Stewart/PowerShell-ADName

With this module installed, you can write code like this:

Get-ADUser -LDAPFilter "(name=*)" |
  Select-Object @{Name = "Path"; Expression = {$_.DistinguishedName | Get-ADName -Format Parent | Convert-ADName Canonical}},
  Name

This example uses Select-Object to create a calculated property called Path that is the AD path of each object expressed in canonical name format.

Inside the Expression = scriptblock (the scriptblock is the code within the curly { } braces), this example uses:

  • $_.DistinguishedName - this is the DistinguishedName property of each user passed from Get-ADUser
  • Get-ADName -Format Parent - the distinguished name's parent location
  • Convert-ADName Canonical - the parent path in canonical format

This command creates output with Path and Name properties; e.g.:

Path                     Name
-----------------------  ---------
myorg.local/Container A  Ken Dyer
myorg.local/Container B  Lynn Dyer

... etc.

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.