Changing File name with PowerShell

Hello everyone,

I am rookie in a power shell, and I will be grateful for some help. I was trying to rename the suffix of filename, but it didn't go very well. Name of my file is: "hello.txt.abcdefg" I want to rename it into "hello.txt". I already tryed something like this:

 Get-ChildItem * | Rename-Item -NewName {$_.name -replace ".%",'.txt'}

 Get-ChildItem * | Rename-Item -NewName {$_.name -replace ".*",'.txt'}

I really don't know what am I doing here, so I will be grateful for a solution and some explanation here.

thanks in advance,

Alex

Answer
Answer
Hi Alex

Can I please ask a question, are you doing this because all your filenames have been appended with an extra file extension, or is there another reason you need a script to do this . . .

The only reason i am asking, is if all your files have been appended with another file extension, that is the sign of a ransomware attack . . .

The script you would be looking for is something like this, please test that on a sample folder of test files, before attempting to run that on a folder of real files . . .

Dir *.* | rename-item -newname { [io.path]::ChangeExtension($_.name, "") }
___________________________________________________________________

Power to the Developer!

MSI GV72 - 17.3", i7-8750H (Hex Core), 32GB DDR4, 4GB GeForce GTX 1050 Ti, 256GB NVMe M2, 2TB HDD

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

Hello Alex,

Option 5 in the tutorial below can show you one way to rename a file along with its file extension using PowerShell.

Rename-Item -Path "Full path of file" -NewName "New Name" -Force

https://www.tenforums.com/tutorials/125119-rename-file-windows-10-a.html#option5

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated June 19, 2019 Views 25 Applies to: