title: How to get path segment using Powershell for renaming
body: <p>I'm trying to make a script where I copy a file from some place and store it in another one. I'm using foreach recursively to get all the files.</p>
  
  <p>The question is: How can I get a path segment and replace it with another string? I want to do something like this:</p>
  
  <p>Copy files from</p>
  
  <pre><code>C:\sites\somefolder\folder1\file.php
  </code></pre>
  
  <p>to </p>
  
  <pre><code>C:\sites\anotherfolder\folder1\file.php
  </code></pre>
  
  <p>In this example, I need to replace 'somefolder' with 'anotherfolder'. This is how I'm getting the full path:</p>
  
  <pre><code>foreach ($i in Get-ChildItem -Recurse -Force) { 
     Write-host $i.Fullname
  }
  </code></pre>
  
  <p>Inside the loop, I tried to replace it using Fullname.replace, but things didn't work as expected:</p>
  
  <pre><code>$i.Fullname.Replace($PWD, "anotherfolder")
  # This returns "anotherfolder/folder1/file.php", 
  # instead of "c:\sites\anotherfolder\folder1\file.php
  </code></pre>
  
  <p>I don't know much of Powershell programming, so bear with me, ok?</p>
  
  <p>Thx!</p>