title: Unable to pass formatted Get-ADOrganizationlUnit output into function in script
body: <p>I'm unable to pass the results of <code>Get-ADOrganizationalUnit</code> into a function in a script.</p>
  
  <p>I am storing the results in a variable that is being used to add the canonical name of the OUs returned by the cmdlet, to a dropdown list on a form.</p>
  
  <p>I'm then attempting to use the same variable in a function that when called, will determine the distinguished of the OU based on the canonical name selected.</p>
  
  <p>Since the variable is being set when the form loads so that the drop down in question will be populated with the various OUs, I added in a <code>write-output $myVar</code> just to make sure nothing strange was going on prior to being passed into the function. I've attempted to make <code>$myVar</code> global with <code>$Global:varName</code> and I've attempted to pass in the variable when calling the function: <code>myFunction $myVar $myVar1</code>. If I then use <code>write-output $myVar</code> within the function, there is no output, but I can use <code>write-host $myVar</code> and it will return a string of just the distinguishedNames of all the OUs in <code>$myVar</code>.</p>
  
  <p>I tested this in the shell directly as well and I didn't have any issues with associating the canonical name back to the DN of the OU, but don't know what I'm doing incorrectly to cause it to not work when used in a script.</p>
  
  <p>I'm using this to get the OU data for the dropdown list and function:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$userOUs = Get-ADOrganizationalUnit -SearchBase $ouRoot -Filter * -Properties CanonicalName | Where-Object {$_.Name -like '*user*'}
  </code></pre>
  
  <p>Note: The dropdown list is populated successfully using <code>$userOUs</code>.</p>
  
  <p>I call the function with a button:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$myBtn.Add_Click({ myFunction $userID $userOUs})
  </code></pre>
  
  <p>The function I'm trying to pass it to:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>
  function myFunction($userID, $userOUs) {
              Write-Output $userOUs #returns nothing
              Write-Host $userOUs #returns the string containing all of the OUs' distinguished names
  
              $selectedOU = $OUList.SelectedItem
              $targetOUCanonicalName = "$domainPrefix$selectedOU" #I remove the domain name from the canonical name for display in the dropdown but add it back here
              $targetOu = $userOUs | Where-Object {$_.CanonicalName -eq $targetCanonicalName} | select -ExpandProperty distinguishedName 
              Get-ADUser -Identity $userID | Move-ADObject -TargetPath $targetOU
  
  }
  </code></pre>
  
  <p>Ultimately, the goal is to be able to use the <code>$userOus</code> variable to determine the DN of the OU based on the selection made in a drop down of OU canonical names. I'd like to try and keep this more dynamic without having to define everything in a switch statement.</p>
  
  <p>I expect once I get a bump in the right direction regarding why the variable is not passing into the function the way that I need it to, I'll be able to accomplish that.</p>
  
  <p>Edit: I don't intend to use <code>write-host</code> or <code>write-output</code> since the script will have a form, I'm just using that to try and figure out what's going on.</p>