Remove character from filename in powershell

I am new to powershell and wanted to know if there is a way to remove the character from the filename. The character I'm trying to remove is the dash "-" and sometimes there are 2 or 3 dashes in the filename. Is it possible to rename files that have a dash in them?

+1


source to share


2 answers


Get-Item .\some-file-with-hyphens.txt | ForEach-Object {
  Rename-Item $_ ($_.Name -replace "-", "")
}

      



This question might be more appropriate for SuperUser.

+6


source


Use single quotes to remove or replace characters in the filename, '

and backslash for "special" characters \

, so the regular expression parser takes it literally.

The following removes the $

(dollar sign) sign from all filenames in your current directory:

Get-Item * | ForEach-Object { rename-item $_ ($_.Name -replace '\$', '') }

      

same as above, using a shorter alias for each command:



gi * | % { rni $_ ($_.Name -replace '\$', '') }

      

Next, the standard character " Z

" is removed from all filenames in your current directory:

gi * | % { rni $_ ($_.Name -replace 'Z', '') }

      

+2


source







All Articles