title: Script for renaming files in a specific way
body: <p>Is there a possibility to build a script (PowerShell for instance) where the file will be renamed with specific text according to specific text?</p>
  <p>I have thousands of files like:</p>
  <ul>
  <li><p>12345_id_user.pdf</p>
  </li>
  <li><p>23456_id_user.pdf</p>
  </li>
  <li><p>34567_id_user.pdf</p>
  </li>
  <li><p>45678_id_user.pdf</p>
  <p>etc.</p>
  </li>
  </ul>
  <p>And I need to change first part (digits) with another specific id:</p>
  <p>12345 → 98765</p>
  <p>23456 → 87654</p>
  <p>34567 → 76543</p>
  <p>I have list (excel/notepad/word) of new ids and old ids that need to be replaced.</p>
  <p>So, is it possible to create a script where it will look for name '12345' compare it to new list and change to '98765'?</p>
  <p>Update 2:</p>
  <pre class="lang-cs prettyprint-override"><code>$Path = &quot;C:\Users\oledv\Desktop\TestEnv\&quot;
  $Text = $arrExcelValuesA
  #$PathArray = @()
  #$Results = &quot;C:\temp\test.txt&quot;
  
  function Release-Ref ($ref) {
      ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
      [System.GC]::Collect()
      [System.GC]::WaitForPendingFinalizers()
  }
  $arrExcelValuesA = @()
  
  $objExcel = New-Object -ComObject Excel.Application
  $objWorkbook = $objExcel.Workbooks.Open(&quot;C:\Users\oledv\Desktop\TestEnv\TestIds.xlsx&quot;)
  $objWorksheet = $objWorkbook.Worksheets.Item(1)
  
  Get-ChildItem $Path -Filter &quot;*.pdf&quot; | ForEach-Object {
      $FileName = Get-Content $_.Name
      Write-Output $FileName
      $i = 1
  
      do {
          $arrExcelValuesA = $objWorksheet.Cells.Item($i, 1).Value()
          $i++
      } while ($FileName -eq $arrExcelValuesA)
  
      $row = $objWorkSheet.Cells.Find($arrExcelValuesA).row
      $col = $objWorkSheet.Cells.Find($arrExcelValuesA).column
      $NewName = $objWorksheet.Cells.Item($row, $col+1).Value()+$arrExcelValuesA.Substring(5)
      Rename-Item -Path $_.Name -NewName $NewName
  }
  </code></pre>
  <p>It renames first file, but looks like it doesn't go through all files in folder...</p>
  <pre>
  Rename-Item : Cannot create a file when that file already exists.
  At C:\Users\oledv\Desktop\TestEnv\Check.ps1:40 char:18
  +                  Rename-Item -Path $_.Name -NewName $NewName
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\Users\oledv\...456_id_user.pdf:String) [Rename-Item], IOException
      + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
  
  Rename-Item : Cannot create a file when that file already exists.
  At C:\Users\oledv\Desktop\TestEnv\Check.ps1:40 char:18
  +                  Rename-Item -Path $_.Name -NewName $NewName
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\Users\oledv\...567_id_user.pdf:String) [Rename-Item], IOException
      + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
  
  Rename-Item : Cannot create a file when that file already exists.
  At C:\Users\oledv\Desktop\TestEnv\Check.ps1:40 char:18
  +                  Rename-Item -Path $_.Name -NewName $NewName
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\Users\oledv\...678_id_user.pdf:String) [Rename-Item], IOException
      + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
  
  Rename-Item : Cannot create a file when that file already exists.
  At C:\Users\oledv\Desktop\TestEnv\Check.ps1:40 char:18
  +                  Rename-Item -Path $_.Name -NewName $NewName
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\Users\oledv\...789_id_user.pdf:String) [Rename-Item], IOException
      + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
  
  Rename-Item : Cannot create a file when that file already exists.
  At C:\Users\oledv\Desktop\TestEnv\Check.ps1:40 char:18
  +                  Rename-Item -Path $_.Name -NewName $NewName
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\Users\oledv\Desktop\TestEnv\xxxx.pdf:String) [Rename-Item], IOException
      + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
  </pre>