title: Get AD distinguished name
body: <p>I'm trying to take input from a CSV file, which has a list of group names (canonical names) and get the Distinguished Name from it, then output to another CSV file. The code:</p>
  
  <pre><code>#get input file if passed    
  Param($InputFile)
  
  #Set global variable to null
  $WasError = $null
  
  #Prompt for file name if not already provided
  If ($InputFile -eq $NULL) {
    $InputFile = Read-Host "Enter the name of the input CSV file (file must have header of 'Group')"
  }
  
  #Import Active Directory module
  Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue
  
  $DistinguishedNames = Import-Csv -Path $InputFile -Header Group | foreach-Object {
    $GN = $_.Group
    $DN = Get-ADGroup -Identity $GN | Select DistinguishedName
  }
  $FileName = "RESULT_Get-DistinguishedNames" + ".csv"
  
  #Export list to CSV
  $DNarray | Export-Csv -Path $FileName -NoTypeInformation
  </code></pre>
  
  <p>I've tried multiple solutions, and none have seemed to work. Currently, it throws an error because</p>
  
  <blockquote>
    <p>Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.</p>
  </blockquote>
  
  <p>I tried using <code>-Filter</code> also, and in a previous attempt I used this code:</p>
  
  <pre><code>Param($InputFile)
  
  #Set global variable to null
  $WasError = $null
  
  #Prompt for file name if not already provided
  If ($InputFile -eq $NULL) {
    $InputFile = Read-Host "Enter the name of the input CSV file(file must have header of 'GroupName')"
  }
  
  #Import Active Directory module
  Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue
  
  $DistinguishedNames = Import-Csv -Path $InputFile | foreach {
    $strFilter = "*"
  
    $Root = [ADSI]"GC://$($objDomain.Name)" 
  
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($root) 
    $objSearcher.Filter = $strFilter 
    $objSearcher.PageSize = 1000
    $objsearcher.PropertiesToLoad.Add("distinguishedname") | Out-Null
  
    $objcolresults = $objsearcher.FindAll() 
    $objitem = $objcolresults.Properties 
    [string]$objDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    [string]$DN = $objitem.distinguishedname
    [string]$GN = $objitem.groupname
  
    #Get group info and add mgr ID and Display Name
    $props = @{'Group Name'= $GN;'Domain' = $objDomain;'Distinguished Name' = $DN;}
    $DNS = New-Object psobject -Property $props 
  }
  $FileName = "RESULT_Get-DistinguishedNames" + ".csv"
  
  #Export list to CSV
  $DistinguishedNames | Sort Name | Export-Csv $FileName -NoTypeInformation
  </code></pre>
  
  <p>The filter isn't the same one I was using here, I can't find the one I was using, the I currently have is a broken attempt.</p>
  
  <p>Anyway, the main issue I was having is that it will get the group name, but search for it in the wrong domain (it wouldn't include Organizational Units) which caused none of them to be found. When I search for a group in PowerShell though (using <code>Get-ADGroup ADMIN</code>) they show up with the correct DN and everything. Any hints or code samples are appreciated.</p>