title: Extract Part of a string in powershell
body: <p>I am trying to extract 2 pieces of information from a string value. The first in from the 4th last to the 2nd last character; the second is from the 2nd last to the last character. This is the code I'm using:</p>
  
  <pre><code>foreach ($item in $List)
  {
      $len = $item.Length
      $folder1 = $item.Substring(($len - 2), $len)
      $folder2 = $item.Substring(($len - 4), ($len - 2))
  
      ..
  } 
  </code></pre>
  
  <p>This code keeps throwing an error on the Substring function. The error description is as below:</p>
  
  <pre><code>*Exception calling "Substring" with "2" argument(s): "Index and length must refer to a
        location within the string.
  Parameter name: length"
  At line:7 char:1
  + $str.Substring($flen - 2, $slen)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentOutOfRangeException*
  </code></pre>
  
  <p>How do I use Substring? What should I pass as the correct parameters?</p>