0

For example, how can I change multiple file names in a folder: which are in the format " New file " to "New file" in windows 10 using powershell

Jiss
  • 11
  • Need to remove trailing and leading spaces, multiple spaces in between words in multiple file names in a folder using powershell. – Jiss Jun 13 '18 at 00:31
  • Possible duplicate of [Removing spaces from a variable thats user inputted in PowerShell 4.0](https://stackoverflow.com/questions/24355760/removing-spaces-from-a-variable-thats-user-inputted-in-powershell-4-0) – snipsnipsnip Jun 13 '18 at 01:26

2 Answers2

1

From https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/18/trim-your-strings-with-powershell/

The easiest Trim method to use is the Trim() method. It is very useful, and it is the method I use most. It easily removes all whitespace characters from the beginning and from the end of a string. This is shown here:

PS C:\> $string = " a String "
PS C:\> $string.Trim()
# => a String

The method is that easy to use. I just call Trim() on any string, and it will clean it up. Unfortunately, the previous output is a bit hard to understand, so let me try a different approach. This time, I obtain the length of the string before I trim it, and I save the resulting string following the trim operation back into a variable. I then obtain the length of the string a second time. Here is the command:

$string = " a String "
$string.Length
# => 10
$string = $string.Trim()
$string
# => a String
$string.Length
# => 8
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
CAMD_3441
  • 2,514
  • 2
  • 23
  • 38
0

try this :

Get-ChildItem "C:\Temp" -Recurse -file | Rename-Item -NewName {"{0}{1}" -f $_.BaseName.Trim(), $_.Extension}
Esperento57
  • 16,521
  • 3
  • 39
  • 45