Renaming files with PowerShell

JOSÉ LUÍS DE BRITO CARDOSO 21 Reputation points
2022-02-01T16:43:50.57+00:00

Hello there,

I'm totally newbie at PowerShell and I'm trying to rename some files with it (there are many folders with many many files each). I found a script out of the box that almost fits my needs, and I was able to change it accordingly. My code that works is:

ls | %{$extension = $.Extension; Rename-Item $ $number$extension; $number++}

$number is set previously = 0. What it does is basically naming the files from 0 to the number of files that the folder has.

I tried to change that line removing the variable, like this:

ls | %{Rename-Item $_ $number$_.Extension; $number++}

But it doesn't work, instead of naming a file from "example.txt" to "0.txt", it goes to "0example.txt.Extension", so it seems to me that the property $_.Extension isn't being read correctly. Can someone please explain to me what is happening?

Windows for business Windows Server User experience PowerShell
Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-02-01T17:33:20.097+00:00

    Hi @JOSÉ LUÍS DE BRITO CARDOSO ,

    please try this:

    $number = 0  
    ls | %{Rename-Item $_ "$number$($_.Extension)";  $number++}  
    

    Just compare this and your approach. It's just a matter of syntax.

    Btw.: Your code might look shorter but is bascically the same:
    ls is an alias of Get-ChildItem
    % is an alias of Foreach-Object

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. JOSÉ LUÍS DE BRITO CARDOSO 21 Reputation points
    2022-02-01T16:47:07.14+00:00

    Code that works:

    ls | %{$extension = $_.Extension; Rename-Item $_ $number$extension; $number++}
    

    Code that doesn't

    ls | %{Rename-Item $_ $number$_.Extension; $number++}
    
    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-02-01T17:10:17+00:00

    Hi @JOSÉ LUÍS DE BRITO CARDOSO ,

    please try this and see if it's working for you:

    $folder = "C:\Junk\3"  
    $count = 0  
    Get-ChildItem -Path $folder | ForEach-Object {  
      $ext = $_.Extension  
      Rename-Item -Path $_.FullName -NewName "$count$ext"  
      $count += 1  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. JOSÉ LUÍS DE BRITO CARDOSO 21 Reputation points
    2022-02-01T17:16:38.86+00:00

    Hi @Andreas Baumgarten ,

    Yes, it does work, but that isn't exactly the point, I'd like to know why the code below doesn't... Since it is basically a shorter version of the code that works, I'm curious what am I missing...

    ls | %{Rename-Item $_ $number$_.Extension; $number++}

    0 comments No comments

  4. JOSÉ LUÍS DE BRITO CARDOSO 21 Reputation points
    2022-02-01T18:04:04.763+00:00

    Hi @Andreas Baumgarten ,

    Oh I see, so it was a syntax problem, this works....

     ls | %{Rename-Item $_ "$number$($_.Extension)";  $number++}  
    

    This doesn't, and I thought they were all the same '-'

     ls | %{Rename-Item $_ "$number($_.Extension)";  $number++}  
      
     ls | %{Rename-Item $_ "$number$_.Extension";  $number++}  
      
     ls | %{Rename-Item $_ $number$_.Extension;  $number++}  
    

    Thank you very much!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.