title: How to properly set the path for executing a child powershell script?
body: <p><strong>Resolved</strong>: the solution to below is to use the following Join-Path:
  <code>[System.Reflection.Assembly]::LoadFrom((Join-Path (pwd) "MyAssembly.dll")) | out-null</code></p>
  
  <p><strong>Original question</strong>:</p>
  
  <p>I have powershell scripts and an assembly file placed as follows:</p>
  
  <pre><code>D:\path1\script1.ps1
  D:\path2\script2.ps1
  D:\path2\MyAssembly.dll
  </code></pre>
  
  <p>Within D:\path1\script1.ps1 I need to call D:\path2\script2.ps1. Then, script2.ps1 will in turn load some C# assemblies that are already placed in the D:\path2 folder. I was under the impression that if I do either Set-Location or Push-Location then the working directory would be properly set for anything that happens within script2.ps1.</p>
  
  <p>script1.ps1 looks like this:</p>
  
  <pre><code>$otherpath = "D:\path2"
  
  Set-Location -Path $otherpath
  
  Push-Location -Path $otherpath
  
  .\script2.ps1
  </code></pre>
  
  <p>script2.ps1 looks like this:</p>
  
  <pre><code>[System.Reflection.Assembly]::LoadFrom("MyAssembly.dll") | out-null
  </code></pre>
  
  <p>However, when I'm in D:\path1, I execute script1.ps1 as follows, and then I get a FileNotFound exception:</p>
  
  <pre><code>D:\path1&gt;powershell .\script1.ps1
  
  Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly
  'file:///D:\path1\MyAssembly.dll' or one of its dependencies. The system cannot find the file
  specified."
  At D:\path2\script2.ps1:1 char:1
  + [System.Reflection.Assembly]::LoadFrom("MyAssembly.dll") | out-null
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : FileNotFoundException
  </code></pre>
  
  <p>How do I set the path correctly such that when I call script1.ps1 from D:\path1 then the environment has D:\path2 set properly as the working directory for all the calls to assemblies in D:\path2?</p>