Mahmoud ElMansy: knowledge meant to be free

Powershell rename all files in a folder

Avery hard question to ask How to rename all files in folder ?
And the answer is very simple use Powershell
I will make a simple code to get all files in the folder and search for word or pattern in the name
And change the name of the file .

$Files = get-childitem -path Fullfolderpath -recurse |
where-object {$_.PsISContainer -ne $true}

foreach ($file in $Files) {
  $matched = $true
  switch -regex ($file)
   {
     'txt'
     {rename-item $_.Fullname -newname ($_ -replace "txt","")}
     'sample'
     {rename-item $_.Fullname -newname ($_ -replace "sample","sample1")}
     'new'
     {rename-item $_.Fullname -newname ($_ -replace "new","old")}
     'default' {$matched = $false}
  } #End switch
} # End foreach

also we can change file extinsions .

$files = Get-Content -recurse
foreach ($file in $files) {
    $newFileName=$file.name.replace(".JPG",".jpg")
    Rename-Item $file $newFileName
}

We can lower case all files .

$Files = get-childitem -path Fullfolderpath  -recurse|
where-object {$_.PsISContainer -ne $true}
foreach ($file in $Files) {
if ((! $file.IsPsContainer) -and $file.Name -cne $file.Name.ToLower()) { ren $file.FullName $file.Name.ToLower() }
}
}

OR using this code .

dir Leaflets -r | % { if ((! $_.IsPsContainer) -and $_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

We are done just run this code and see.