Vaccano Vaccano - 1 year ago 99
PowerShell Question

Rename parts of file names that match a pattern

Say I have a list of file names like this:


I need to change the file names that have
in them to have just that part renamed to

would become

And I need to do this recursively from a root directory.

I seem to be to be hopeless at PowerShell so I thought I would ask here to see if anyone can help me out.

Answer Source

Use the Get-ChildItem cmdlet with the -recurse switch to get all items recursivly from a root directory. Filter all items containing my-project using the Where-Object cmdlet and finally rename them using the Rename-Item cmdlet:

Get-ChildItem "D:\tmp" -Recurse | 
    Where {$_.Name -Match 'my-project'} | 
    Rename-Item -NewName {$ -replace 'my-project','$appname$' } 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download