title: Parsing variables into Get-ADUser CMDLT - Error parsing query
body: <p>I am attempting to write some scripts to match a partial AD user account name into a get-aduser script to return the objects for another routine.</p>
  
  <p>However, the command when executed it returning a parse error</p>
  
  <p>The strange thing is that when checking the syntax of the output, it looks AOK:</p>
  
  <blockquote>
    <p>PS C:\Users> <strong>$ADUserString</strong> = "dcro"</p>
    
    <p>PS C:\Users> <strong>write-host get-aduser</strong> -filter "{SAMAccountName -like '"$ADUserString*'"}"</p>
    
    <p>get-aduser -filter {SAMAccountName -like "dcro*"}</p>
  </blockquote>
  
  <p>Note: I have used the right-tick character ` prepeding the quotes on the variable to keep them as a string value</p>
  
  <p>So when executing the command:</p>
  
  <blockquote>
    <p>PS C:\Users> get-aduser -filter "{SAMAccountName -like <code>"$ADUserString*</code>"}"</p>
    
    <p><em>get-aduser : <strong>Error parsing query:</strong> '{SAMAccountName -like "dcro</em>"}' Error Message: 'syntax error' at position: '1'.
    At line:1 char:1
    + get-aduser -filter "{SAMAccountName -like <code>"$ADUserString*</code>"}"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
        + FullyQualifiedErrorId : Error parsing query: '{SAMAccountName -like "dcro*"}' Error Message: 'syntax error' at position: '1'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser*</p>
  </blockquote>
  
  <p>However, if I manually type the output from my 'write-host' above, it executes perfectly fine and returns the results I am after:</p>
  
  <blockquote>
    <p>PS C:\Users> get-aduser -filter {samaccountname -like "dcro*"}</p>
    
    <p>DistinguishedName : CN=Dan*****,OU=A*****port,OU=Development*****************</p>
    
    <p>Enabled           : True</p>
    
    <p>GivenName         : D****</p>
    
    <p>Name              : D*****Cro****</p>
    
    <p>ObjectClass       : user</p>
    
    <p>ObjectGUID        : 796b**********413-558d*****d73</p>
    
    <p>SamAccountName    : dcro*****</p>
    
    <p>SID               : S-1******************67</p>
    
    <p>Surname           : Cro******</p>
    
    <p>UserPrincipalName : dcro***********</p>
  </blockquote>
  
  <p>It's pretty odd, and my feeling is that there are some weird special characters at play here.....</p>