-1

I have a list of hundreds of files in a folder at path C:\Users\Files The current files are all named with a leading ID number that is five digits and then continues with the rest of the filename and extension. Using Powershell, I want to add an underscore after the 5th character for all files in this directory.

Before: 12345DocumentA.pdf

After: 12345_DocumentA.pdf

Any help or guidance is appreciated! Thanks, Jenn

Jenn
  • 35
  • 1
  • 9
  • Does this answer your question? [Issue attempting to insert character into filename](https://stackoverflow.com/questions/62925177/issue-attempting-to-insert-character-into-filename) – zett42 Mar 05 '21 at 07:49
  • Yes that answers my question thank you. I did search for a bit of time before posting so I'm not sure why someone would mark me a minus for this post. I thought asking questions is what this forum was about. – Jenn Mar 05 '21 at 19:42

1 Answers1

0

There are more ways to do this, but this is one:

(Get-ChildItem -Path 'D:\Test' -Filter '*.pdf' -File) |
    Where-Object { $_.Name -match '^\d{5}[^_]+' } |              # name should start with 5 digits, NOT followed by an underscore
    Rename-Item -NewName { $_.Name -replace '^(\d{5})', '$1_' }
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks Theo. This probably does what I was wanting but someone provided a link to a previous post from someone else and I was able to make that work. I really appreciate the help! – Jenn Mar 05 '21 at 19:43