title: Can't find type [Microsoft.ActiveDirectory.Management.ADAccount] when deploying script using Task Scheduler
body: <p>I have a Powershell script version 5.1 that works when run using the Powershell Window, but get the following error when the script is run from a batch file.  I am trying to use proper typing for elements of a class I created.  I get the following error:</p>
  <pre><code>At C:\scripts\Upload_to_Orientation\TestStubs\myTest.ps1:76 char:3
  +     [Microsoft.ActiveDirectory.Management.ADAccount]$ADUser
  +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Unable to find type [Microsoft.ActiveDirectory.Management.ADAccount].
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : TypeNotFound
  </code></pre>
  <p>I've tried adding the following, but the class is still not defined:</p>
  <pre><code>using assembly 'C:\Windows\Microsoft.NET\assembly\GAC_64\Microsoft.ActiveDirectory.Management\v4.0_10.0.0.0__31bf3856ad364e35\Microsoft.ActiveDirectory.Management.dll'
  using namespace Microsoft.ActiveDirectory.Management
  </code></pre>
  <p>Here is a stripped down version of the script</p>
  <pre><code>Add-PSSnapin &quot;Microsoft.SharePoint.Powershell&quot;
  #The following make no difference...
  Import-module ActiveDirectory
  
  class GenUser {
      [string]$Pwd 
      [string]$Status
      [string]$ErrMsg
      
      #[Microsoft.SharePoint.SPUser]$SPEmployee
      [Microsoft.ActiveDirectory.Management.ADUser]$ADUser
      #[Microsoft.ActiveDirectory.Management.ADAccount]$ADUser
  
      [string]$FirstName
      [string]$LastName
      [string]$Name
      [string]$DisplayName
      [string]$EmployeeNumber
      [string]$Email
      [string]$SamAccount
  
      GenUser([string]$first_name, [string]$last_name) {
          $n = $first_name + &quot; &quot; + $last_name
          $this.Status = &quot;unknown&quot;
          $this.ErrMsg = &quot;&quot;
          $this.FirstName = $first_name
          $this.LastName = $last_name
          $this.Name = $n
          $this.DisplayName = $n
          $this.Email = &quot;&quot;
          $this.SamAccount = &quot;&quot;
          $this.Pwd = &quot;&quot;
      }
      
      GenUser([Microsoft.ActiveDirectory.Management.ADUser]$AD_User) {
          $this.Status = $AD_User.Enabled
          $this.ErrMsg = &quot;&quot;
          #$this.ADUser = $AD_User
          $this.FirstName = $AD_User.GivenName
          $this.LastName = $AD_User.Surname
          $this.Name = $AD_User.Name
          $this.DisplayName = $AD_User.DisplayName
          $this.Email = $AD_User.UserPrincipalName
          $this.SamAccount = $AD_User.SamAccountName
          $this.Pwd = &quot;&quot;
  
      }
  
  #Note: I stripped out most of my methods
  
  }
  
  
  [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
  {
      $logfile = &quot;C:\scripts\Upload_to_Orientation\TestStubs\myTest2.log&quot;
      Write-Output &quot;Starting myTest.ps1 ... &quot; | Out-File $logfile -Encoding ASCII
      Write-Host &quot;Starting...&quot;
  
      Write-Output &quot;PSVersion = $($PSVersionTable['PSVersion'])`r`n&quot; | Out-File $logfile -Append -Encoding ASCII
      
      $test = [GenUser]::New(&quot;John&quot;, &quot;Doe&quot;)
      
      Write-Output &quot;Name = $($test.Name) &quot; | Out-File $logfile -Append -Encoding ASCII
  
  
      Write-Output &quot;Script completed ... &quot; | Out-File $logfile -Append -Encoding ASCII
      Write-Host &quot;Done...&quot;
  })
  </code></pre>
  <p>What am I missing?  It works fine when run from the Powershell window after I run &quot;Import-Module ActiveDirectory&quot;, but I need to add the script to the Task Scheduler using a batch file to start the script so I can redirect errors to a second log file.</p>
  <p>My batch file is:</p>
  <pre><code>cd C:\scripts\Upload_to_Orientation\TestStubs &amp;&amp; powershell.exe -F .\myTest2.ps1 &gt;m2.log  2&gt;&gt;&amp;1
  </code></pre>