Search code examples
powershellrenamepipeline

Rename-Items twice in the same pipeline using powershell


I'm trying to rename a bunch of files using powershell by removing the prefix (which is the same in all files), replacing "+" with a space and setting the remainder to title case. Here's what I have so far:

Where-Object { $_ -Match '^Website\.com_+' } |
ForEach-Object
{
    $_ |
    Rename-Item -NewName {$_.Name -replace 'Website\.com_','' -replace '\+',' '};
    Rename-Item $_.Fullname (Get-Culture).TextInfo.ToTitleCase($_)
}

The first rename works, it removes and formats files properly, but then the second rename says the items don't exist, which makes me think I should just then pass them into another foreach loop in another pipe, but I can't seem to make that work either. It seems like having 2 rename-items isn't really working and I tried having the title case with the replace and it doesn't seem to work either.


Solution

  • Finally found something that works:

    $path = "G:\Downloads\Chrome\test2"
    $items = (Get-ChildItem -Path $path -File *.mp4 | 
    Where-Object { $_ -Match '^Website\.com_+' } |
    ForEach-Object{
        $_ |
        Rename-Item -NewName {$_.Name -replace 'Website\.com_','' -replace '\+',' '} -PassThru
    })
    $items | % {
        Rename-Item -Path $_.FullName  -NewName (Get-Culture).TextInfo.ToTitleCase($_.Name)
    }
    

    Thanks to everyone who tried to help me!