title: Powershell commands not working in Build Events in Visual Studio
body: <p>When I run a file through the <strong>Build Events in Visual Studio</strong> I get the same error for EVERY in-built powershell command I try.</p>
  
  <p><strong>The build events window in Visual Studio</strong></p>
  
  <p><img src="https://i.stack.imgur.com/g2xqP.png" alt="enter image description here"></p>
  
  <p><strong>The error</strong></p>
  
  <blockquote>
    <p>------ Build started: Project: XAzure, Configuration: Debug Any CPU ------
     XAzure-> C:\visual studio 2013\Projects\test\bin\XAzure.dll</p>
    
    <p><em>Get-Date : The term 'Get-Date' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.</em></p>
    
    <p>At C:\visual studio 2013\Projects\XAzure\WaitForFiles.ps1:3 char:15
     + <strong>$dateTime = $(Get-Date)</strong>
     +               ~~~~~~~~
         + CategoryInfo          : ObjectNotFound: (Get-Date:String) [], CommandNotFoundException
         + FullyQualifiedErrorId : CommandNotFoundException</p>
    
    <p>========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========</p>
  </blockquote>
  
  <p>The file only contains this one line <em>$dateTime = $(Get-Date)</em> and fails.</p>
  
  <p>It <em>does not matter</em> what command I run</p>
  
  <ul>
  <li>Start-Sleep -Seconds 2</li>
  <li>Write-Output</li>
  <li>Get-Date</li>
  <li>etc.</li>
  </ul>
  
  <p>But if I switch out Get-Date for e.g </p>
  
  <pre><code>$start =[System.DateTime]::Now.ToString("yyyy-MM-dd-HH-mm-ss.fff") 
  </code></pre>
  
  <p>it works just fine. But that is no solution.</p>
  
  <p>I´m firing this up in the Build Events with </p>
  
  <pre><code>Powershell -ExecutionPolicy bypass -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\" 
  </code></pre>
  
  <p>But I have also tried number of other variations like (can´t remeber them all)</p>
  
  <pre><code>Powershell -ExecutionPolicy Unrestricted -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\"
  </code></pre>
  
  <p><strong>Runing this file in a powershell window works just fine</strong> so I must have the commands but not in the build events. </p>
  
  <p>I´m running <em>Visual Studio 2013 update 4 in <strong>admin mode</em></strong> on a win32 Windows 8.1 i7 Dell Laptop.</p>
  
  <p>Running <em>$psversiontable</em> gives me</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      4.0
  WSManStackVersion              3.0
  SerializationVersion           1.1.0.1
  CLRVersion                     4.0.30319.34014
  BuildVersion                   6.3.9600.17090
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
  PSRemotingProtocolVersion      2.2
  </code></pre>
  
  <p>I have tried to give powershell more access and elevated privileges but nothing has worked. </p>
  
  <p>I can´t think of anything more to add to this question or to try. I googled this for 3 hours this weekend and tried everything that I could find on this error with out success. </p>
  
  <p>The strange thing is that there are lots of this "<em>The term 'x' is not recognized..</em>" error out there with seemingly totally different causes. </p>
  
  <p><strong>Update 1</strong></p>
  
  <p>I have tried to explicitly "install" Microsoft.PowerShell.Utiltiy (in a admin Powershell window ) with this command 
      Import-Module Microsoft.PowerShell.Utility</p>
  
  <p>but I get this error</p>
  
  <blockquote>
    <p><em>Import-Module : The specified module 'Microsoft.PowerShell.Utility' was not loaded because no valid module file was found in any module directory.</em></p>
  </blockquote>
  
  <p>If I run $env:PSModulePath I get</p>
  
  <blockquote>
    <p>c:\Program Files\AppFabric 1.1 for Windows Server\PowershellModules;</p>
    
    <p>C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement</p>
  </blockquote>
  
  <p><strong>BUT</strong> if I run %windir%\system32\WindowsPowerShell\v1.0\Modules 
  I find <em>C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules</em></p>
  
  <p><strong>Does that mean that the this set of modules are not connected somehow?</strong> I see the Microsoft.PowerShell.Utiltiy folder there.</p>