Remove leading Zero from file names with Powershell

1.4k Views Asked by At

Good morning.

I am having trouble understanding past topics. I am a new user with minimal understanding.

My file path that I would like to remove all leading zeros from the file names in it is this.

CD Documents\Attachments

Basically what I want to do is copy paste that file path into Powershell then copy paste a command to remove all leading zeroes from the file names in that folder.

and example of the file name would be 0123456789xxxxxxxxxx (length and characters vary)

I would like 123456789xxxxxx

Any help is greatly appreciated.

1

There are 1 best solutions below

4
On BEST ANSWER

As Lee_Dailey comments, you can use the TrimStart() method to trim leading characters off a string:

# Change location to the folder
cd Documents\Attachments

# Discover all the files in the current folder with leading 0s in their name
$files = Get-ChildItem -File -Filter 0*

# Use `Rename-Item` with `TrimStart` to rename the files to the appropriate name without the leading 0s
$files |Rename-Item -NewName { $_.Name.TrimStart('0') }