title: Running a command on each directory in a list using PowerShell
body: <p>I've got a need to make a .zip of each directory in a list in PowerShell. I for some reason cannot figure out how to change to each directory to run a command relative to that path though.</p>
  
  <p>Here is my situation:</p>
  
  <pre><code>$dir = Get-ChildItem d:\directory | ? {$_.PSIsContainer}
  
  $dir | ForEach-Object {Set-Location $_.FullName;
  Invoke-Expression "7z.exe A" +  $_.Name + ".rar " + $_.Path + "\"}
  </code></pre>
  
  <p>The command is ugly, but due to the way that 7Zip seems to parse text, I had to go this way. I believe the command should make a ZIP file in each directory, with the name set equal to the directory name, and include all of the files in the directory.</p>
  
  <p>It seems like I'm stuck in some PowerShell hell though where I cannot even access the values of objects for some reason.</p>
  
  <p>For instance, if I echo $dir, I see my list of directories. However, if I try</p>
  
  <pre><code>gci $dir[1]
  </code></pre>
  
  <p>PowerShell returns nothing.  It's not actually enumerating the directory path contained within the variable property, but instead trying to list the items contained within that value, which would of course be empty.</p>
  
  <p>What gives?! How do I do this?</p>