A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
Hi,
if you want see only OU try following demo:
# Load the Assemblies for the GUI and modules
Add-Type -AssemblyName PresentationFramework
Import-Module ActiveDirectory
[XML]$xamlWindow = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PowerShell OU's" Height="400" Width="400">
<Grid>
<TreeView Name="Tree"/>
</Grid>
</Window>
'@
$xamlReader = (New-Object System.Xml.XmlNodeReader $xamlWindow)
try{
$MainForm=[Windows.Markup.XamlReader]::Load( $xamlReader )
}catch{
Write-Host "Unable to load Windows.Markup.XamlReader. Some problem is there. Please check the XAML code entered."
exit
}
$Tree = $MainForm.FindName('Tree')
$domainDN = 'OU=OU0,DC=lg,DC=loc'
# Functions
Function Add-OUItem ($Name, $Parent, $Tag) {
#Add new TreeViewItem
$ChildItem = New-Object System.Windows.Controls.TreeViewItem
$ChildItem.Header = $Name
$ChildItem.Name = ""
$ChildItem.Tag = "OU=$Name," + $Tag
[Void]$ChildItem.Items.Add("*")
[Void]$Parent.Items.Add($ChildItem)
}
Function Get-DomainOU ($domainPath, $TreeItem){
$sb = $domainPath + $domainDN
$query = Get-ADOrganizationalUnit -SearchBase $sb -LDAPFilter '(name=*)' -SearchScope 1
foreach($item in $query)
{
if($TreeItem.Header -ne $Item.Name)
{
if($Item.ObjectClass -eq 'organizationalUnit')
{Add-OUItem -Name $Item.Name -Parent $TreeItem -Tag $domainPath}
}
}
}
# Operations
foreach($item in (Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $domainDN -SearchScope 1)){
Add-OUItem -Name $item.Name -Parent $Tree -Tag ""
}
$MainForm.Add_SourceInitialized({
[System.Windows.RoutedEventHandler]$Event = {
if($_.OriginalSource -is [System.Windows.Controls.TreeViewItem]){
$TreeItem = $_.OriginalSource
$TreeItem.items.clear()
Get-DomainOU -domainPath $TreeItem.Tag -TreeItem $TreeItem
Write-Host $TreeView
}
}
$Tree.AddHandler([System.Windows.Controls.TreeViewItem]::ExpandedEvent,$Event)
})
$MainForm.ShowDialog() | Out-Null
Result: