I am trying to use PowerShell to set a condition that if a file name contains any of the following symbols (#
, %
, &
. +
. {, }
, ~
),then replace those symbols with "_
". I want to apply this to all file and folder names(including files within sub-folders).
I have below code, which partially works, as it only works for replacing 2 symbols "(%, &)". If i want to replace more symbols it doesn't work. An error message pops up and only the first symbol ends up being replaced. Is there a way to fix this code?
Get-ChildItem -Recurse | `
Where-Object {$_.Name -match '&' -or $_.Name -match '%' } | `
Rename-Item -NewName { $_.Name -replace '&','_' -replace '%','_' }
As i have no experience in coding, i would really appreciate if you provide the entire code that would fix this issue, thank you!