Save the following into a file Set-DosFileName.ps1
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[parameter(Mandatory=$true)]
[string]$folder,
[switch]$recurse
)
$fso = New-Object -ComObject Scripting.FileSystemObject
Get-ChildItem -Path $folder -File -Recurse:$recurse | ForEach-Object {
$shortName = $fso.getfile($_.Fullname).ShortName
if ($shortName -ne $_.Name)
{
$fullShortName = Join-Path $_.Directory -ChildPath $shortName
Move-Item -LiteralPath $_.Fullname -Destination $fullShortName
}
}
To use this open a PowerShell window and change into the directory where you saved the file:
cd "D:\folder where you saved the script"
then:
.\Set-DosFileName.ps1 -folder "D:\myfiles\Foo Bar" -whatif
The script should show how it would rename your files.
To include all files in subdirectories add the -recurse switch:
.\Set-DosFileName.ps1 -folder "D:\myfiles\Foo Bar" -whatif -recurse
If everything looks fine, remove the -whatif switch to actually rename the files. I would still keep a backup of the original files just in case anything goes wrong.
I haven't tested this with a large number of files, be aware that some file names may be pretty ugly.