title: How to specify a subfolder of Inbox using Powershell
body: <p>I'm trying to access a subfolder of "Inbox" named "subfolder" in outlook (2010) using Powershell.</p>
  
  <pre><code>$olFolderInbox = 6
  $outlook = new-object -com outlook.application;
  $ns = $outlook.GetNameSpace("MAPI");
  $inbox = $ns.GetDefaultFolder($olFolderInbox)
  
  # how do I specify a subfolder that's inside Inbox???
  # I mean, "Inbox\subfolder" where "subfolder" is the name of the subfolder...
  </code></pre>
  
  <p>How do I specify this subfolder?</p>
  
  <p>I'm sure this is really simple, which is why I am about to "lose it."  Thanks in advance!</p>
  
  <p>*Later in my code, 
  I search the body for a "searchterm" and send the results to a text file if there's a match.  The following code works for my Inbox:</p>
  
  <pre><code>$inbox.items | foreach {
  if($_.body -match "searchterm") {$_.body | out-file -encoding ASCII foo.txt} # prints to file...
  </code></pre>
  
  <p>Instead of the inbox, I want to look at the subfolder of inbox as described above...</p>
  
  <p>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p>EDIT:</p>
  
  <pre><code>$olFolderInbox = 6
  $outlook = new-object -com outlook.application;
  $ns = $outlook.GetNameSpace("MAPI");
  $inbox = $ns.GetDefaultFolder($olFolderInbox)
  $targetfolder = $inbox.Folders | where-object { $_.name -eq "Subfolder" }
  $targetfolder.items | foreach {
  if($_.body -match "keyword") {$_.body | out-file -Append -encoding ASCII foo.txt} # keyword match prints body to file...
  }
  </code></pre>
  
  <p>OK, I think this works now...</p>
  
  <p>I don't know what I was doing wrong, although it's literally my first day using Powershell, so it's no surprise, really.</p>