Using PowerShell to remove file extension

  Kiến thức lập trình

I’m trying to clean up some folders in Windows that have dozens of text files with a double .txt extension. (file1.txt.txt) I thought the PowerShell script below would work, but it’s giving me errors.

Get-ChildItem -Filter *.txt -Recurse -File -Name | ForEach-Object {Rename-Item $_.BaseName}

Rename-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At line:1 char:80

  • … *.txt -Recurse -File -Name | ForEach-Object {Rename-Item $_.BaseName}

Based on the error saying the Path is null, I added “-Path . ” to the command but it returns the same error message.

I’ve looked at the following post, but the suggestions shown are not working for me.
Removing path and extension from filename in PowerShell

Get-ChildItem -Filter *.txt -Recurse -File -Name | ForEach-Object {Rename-Item $_.BaseName}

Rename-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At line:1 char:80

  • … *.txt -Recurse -File -Name | ForEach-Object {Rename-Item $_.BaseName}

Based on the error saying the Path is null, I added “-Path . ” to the command but it returns the same error message.

LEAVE A COMMENT