Use Regex / Powershell to rename files

Try this one:

Get-ChildItem directory `
        | Rename-Item -NewName { $_.Name -replace '^filename_+','' -replace '_+',' ' }

Note that I just pipe the objects to Rename-Item, it is not really needed to do it via Foreach-Object (alias is %).

Update

I haven’t anything documented about the ‘magic’ with scriptblocks. If I remember correctly, it can be used if the property is ValueFromPipelineByPropertyName=$true:

function x{
    param(
        [Parameter(ValueFromPipeline=$true)]$o,
        [Parameter(ValueFromPipelineByPropertyName=$true)][string]$prefix)
    process {
        write-host $prefix $o
    }
}
gci d:\ | select -fir 10 | x -prefix { $_.LastWriteTime }

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)