PowerShell: How to Move and Rename a File


You can use the following syntax to move and rename multiple files from one folder to another using PowerShell:

Get-ChildItem *.txt | 
    ForEach-Object{
        $newName = $_.FullName -replace 'information', 'info'
        Rename-Item $_ $newName
        Move-Item $newName -destination "c:\users\bobbi\data2"
}

This particular example renames all .txt files in the current directory by replacing “information” with “info” in the file names and then moves them to a new folder named data2.

All files that do not have a .txt extension in the current directory will remain unchanged.

The following example shows how to use this syntax in practice.

Example: How to Move and Rename Files in PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\data1

We can type Get-ChildItem to list out all files in this current directory:

Suppose that we would like to rename all files with a .txt extension by replacing the string “information” with “info” in each file name, then move each of these files to a new folder name data2.

We can use the following syntax to do so:

Get-ChildItem *.txt | 
    ForEach-Object{
        $newName = $_.FullName -replace 'information', 'info'
        Rename-Item $_ $newName
        Move-Item $newName -destination "c:\users\bobbi\data2"
}

The following screenshot shows how to use this syntax in practice:

PowerShell move and rename files

When we use the Get-ChildItem cmdlet to view the contents of the data2 folder, we can see that each of the .txt files from the data1 folder have been renamed by replacing the “information” string with “info” instead.

If we use the Get-ChildItem cmdlet once again to view the files in our current directory, we will see that each of the .txt files that we renamed are no longer in the directory:

Only the .csv files still exist in the current directory, which we did not rename or move.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Rename File Extension of Multiple Files
PowerShell: How to Rename Multiple Folders
PowerShell: How to Copy and Rename a File

Leave a Reply