title: PowerShell - Loop through files and rename
body: <p>newbie here. I am trying to write a PowerShell script to:</p>
  
  <ol>
  <li>loop through all files in directory</li>
  <li>List item</li>
  <li><p>Get all .pdf files ONLY </p>
  
  <p>Rename them-the file names are long - over 30 chars 
  -They contain 2 numbers which I need to extract
  -Example: </p>
  
  <p>Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf -> 
  RESULT : = 18CU11.pdf</p></li>
  </ol>
  
  <p>I tried examples from bunch of sites and I can't seem to even loop successfully.
  Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename</p>
  
  <pre><code>Get-ChildItem "C:\Users\******\Desktop\PowerShell Practice" -Filter *.pdf |  #create list of files
  
  ForEach-Object{
      $oldname = $_.FullName;
      $newname = $_.FullName.Remove(0,17); 
      #$newname = $_.FullName.Insert(0,"CU")
  
      Rename-Item $oldname $newname;
  
      $oldname;
      $newname;  #for testing
  }
  </code></pre>
  
  <p>That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job. </p>