title: Cannot load SharePoint powershell cmdlets in visual studio pre-deployment script
body: <p>
  </p>
  
  <p>While working on a SharePoint 2010 solution in Visual Studio 2012, I want to run a PowerShell script to remove a lookup field from one of my lists, allowing Visual Studio to automatically resolve the script deployment conflict by deleting the existing list and replacing it with the one in my solution.</p>
  
  <p>However, when running PowerShell from the project's "Pre-deployment Command Line" in Visual Studio, when my script attempts to use <code>get-spweb</code>, PowerShell reports that the object is not found.  Scrolling upward in the Visual Studio's output window, I see that <code>Add-PsSnapin Microsoft.SharePoint.PowerShell</code> is reporting various problems:</p>
  
  <p><em>(Note: actual error message has the expanded <code>$(ProjectDir)</code> value rather than "$(ProjectDir)" as text.  Trying various levels of indirection to ensure I'm using the correct, 64-bit version of PowerShell does not change this working directory, nor does using <code>cd</code> or <code>set-location</code> commands prior to calling my script make any difference in this directory.  I'm wondering if an invalid working directory is part of the problem...)</em></p>
  
  <p>
  </p>
  
  <pre class="lang-ps prettyprint-override"><code>The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.
  Could not read the XML Configuration file in the folder CONFIG\PowerShell\Registration\.
  Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\Registration'.
  No xml configuration files loaded.
  Unable to register core product cmdlets.
  Could not read the Types files in the folder CONFIG\PowerShell\types\.
  Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\types'.
  "No Types files Found."
  Could not read the Format file in the folder CONFIG\PowerShell\format\.
  Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\format'.
  No Format files Found.
  </code></pre>
  
  <p>Running the PowerShell script directly from a Command Prompt window works fine, the SharePoint snapin loads correctly, but running from Visual Studio always fails.  Previous research indicated potential problems with SharePoint and SQL Server permissions, yet my account has full admin in both.  Visual Studio is running "as administrator".  Research also turned up possible problems with 32-bit vs 64-bit.  My Pre-deployment Command Line now calls <code>%comspec% /c</code> to ensure 64-bit.</p>
  
  <p>Pre-deployment Command Line:</p>
  
  <p>
  </p>
  
  <pre class="lang-ps prettyprint-override"><code>%comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd" "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
  </code></pre>
  
  <p>*.cmd file:</p>
  
  <p>
  </p>
  
  <pre class="lang-ps prettyprint-override"><code>echo off
  rem pre-deployment script
  rem call from pre-deployment command line like
  rem      %comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
  rem 
  echo Running "predeployment-command.cmd" file
  echo
  echo %%1 = %~1
  echo %%2 = %~2
  echo %%3 = %~3
  echo
  cd "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14"
  cd
  echo on
  powershell.exe -file "%~3PowerShell Scripts\predeployment-script.ps1" -targetWeb "%~1" -CONFIG "%~2"
  </code></pre>
  
  <p>PowerShell script file:</p>
  
  <pre class="lang-ps prettyprint-override"><code>&lt;#
  .SYNOPSIS
      Pre-deployment script for Visual Studio development deployments
      add command to run this script in the project's pre-deployment command-line box
  
  .EXAMPLE
      powershell .\dev-predeployment-script.ps1
  #&gt;
  param(
  
     [string] $targetWeb = $(throw "Please specify the site to which Visual Studio is deploying!"),
  
     [string] $CONFIG = $(throw "Please specify the active configuration!")
  
     )
  write-host "Running Pre-Deployment PowerShell Script";
  
  # Ensure SharePoint extensions are loaded
  $snapin = $(Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue");
  if($snapin -eq $null) {
      Add-PsSnapin Microsoft.SharePoint.PowerShell;
  }
  
  $ErrorActionPreference = 'Stop';
  
  #echo back parameter values:
  write-host "TargetWeb = $targetWeb"
  write-host "Configuration = $CONFIG"
  
  #get web-site
  $web = get-spweb $targetWeb;
  
  if($web -ne $null) {
      $recipients = $web.lists["Recipients"];
  
      if($recipients -ne $null) {
          $lookupField = $recipients.Fields["Request ID"];
  
          if($lookupField -ne $null) {
              $recipients.fields.remove($lookupField);
          }
      }
  }
  </code></pre>
  
  <p>I'm developing on a 64-bit Windows Server 2008 r2 virtual machine.</p>