#OUADSPath2Clip.ps1 #This is a demo of fast OU navigation #alan dot kaplan at va dot gov #6-10-2013 Import-Module ActiveDirectory #Use VB for MsgBox and InputBox [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') Function NavOU { param([string] $adsPath) # onelevel for speed. $server is a GC $a = Get-ADOrganizationalUnit -searchscope OneLevel -server $server -searchbase $adsPath -Filter 'Name -like "*"' ` | Select-Object -Property name, distinguishedname | ogv -OutputMode Single -Title "Select an OU and click OK" if ($a.length -eq 0) {Exit} $global:adsPath = ($a).distinguishedname $Message = 'The currently selected path is ' + ($a).distinguishedname + '. Continue Navigation?' $retval = [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'YesNo,systemmodal,Question',"Continue Search") if ($retval -eq 'Yes') {NavOU ($a).distinguishedname} } Function GetAdsPath(){ $adDomain = Get-AdDomain $DomName = [Microsoft.VisualBasic.Interaction]::InputBox("Choose Starting Domain:", "Domain Name", $adDomain.dnsroot ) if ($Domname.Length -eq 0) {Exit} #Get a Global Catalog Server $adDomain = Get-AdDomain $domname $gc = get-addomaincontroller -server $DomName -Filter { isGlobalCatalog -eq $true} $Global:server = $gc.Item(0).HostName #Write-Host $Server $adspath = $adDomain.DistinguishedName $Message = 'Do you want to select an Organizational Unit of ' + $adspath + '?' $retval = [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'YesNo,systemmodal,Question','Navigate OU Structure?') if ($retval -eq 'Yes') { #Initial Path navOU $adsPath } else { $global:adsPath = $adDomain.DistinguishedName } } getAdsPath #$adsPath = $adsPath.Trim() $Message = 'Do you want to copy ' + $adsPath + ' to the clipboard?' $retval = [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'YesNo,systemmodal,Question','Copy to Clipboard?') if ($retval -eq 'Yes') { $adsPath | clip Write-Host "Done. " $adsPath " copied to the clipboard." }