How to Rename a Folder in PowerShell if It Exists?

Recently, I got a requirement to rename a backup folder if it existed before running a script to create a new backup. In PowerShell, it is easy to do. In this tutorial, I will explain how to rename a folder in PowerShell if it exists.

First, we will check if a folder exists using PowerShell and then see how to rename a folder in PowerShell.

Check if a Folder Exists using PowerShell

The first step is to check if the folder exists. PowerShell provides the Test-Path cmdlet for this purpose. Here’s how you can use it:

$folderPath = "C:\Backup\OldBackup"
if (Test-Path -Path $folderPath) {
    Write-Output "Folder exists."
} else {
    Write-Output "Folder does not exist."
}

In this example, I am checking if the folder C:\Backup\OldBackup exists. If it does, a message is printed to the console.

Check out How to Find The Largest Folders Using PowerShell?

Rename the Folder using PowerShell

Once we have confirmed that the folder exists, we can proceed to rename it using the Rename-Item cmdlet. Here’s the PowerShell syntax:

Rename-Item -Path "C:\Backup\OldBackup" -NewName "OldBackup_20241205"

This command renames the folder C:\Backup\OldBackup to OldBackup_20241205. The new name includes a timestamp for easy identification.

Check out PowerShell Copy-item Create Folder If Not Exist

Rename a Folder in PowerShell if It Exists

Now, let us combine both things, and we will see how to check and rename operations using PowerShell.

To streamline the process, we can combine the check and rename operations into a single script. Here’s a complete example:

$folderPath = "C:\Backup\OldBackup"
$newFolderName = "OldBackup_20241205"

if (Test-Path -Path $folderPath) {
    Rename-Item -Path $folderPath -NewName $newFolderName
    Write-Output "Folder renamed to $newFolderName."
} else {
    Write-Output "Folder does not exist."
}

In this script, I first check if the folder exists. If it does, I rename it and print a confirmation message.

It is also ideal for handling errors while renaming a folder using PowerShell.

Here is the complete PowerShell script. PowerShell provides the Try-Catch block for error handling. Here’s how you can use it:

$folderPath = "C:\Backup\OldBackup"
$newFolderName = "OldBackup_20241205"

try {
    if (Test-Path -Path $folderPath) {
        Rename-Item -Path $folderPath -NewName $newFolderName
        Write-Output "Folder renamed to $newFolderName."
    } else {
        Write-Output "Folder does not exist."
    }
} catch {
    Write-Output "An error occurred: $_"
}

In this script, any errors encountered during the rename operation are caught and printed to the console.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

Rename a Folder in PowerShell if It Exists

Read Get the First 10 Files in a Folder Using PowerShell

Rename Multiple Folders using PowerShell

Let’s say you have multiple backup folders that you want to rename. You can use a loop to iterate through each folder and rename it if it exists. Here’s how:

$folders = @("C:\Backup\OldBackup1", "C:\Backup\OldBackup2", "C:\Backup\OldBackup3")
$dateSuffix = (Get-Date).ToString("yyyyMMdd")

foreach ($folder in $folders) {
    if (Test-Path -Path $folder) {
        $newFolderName = "$folder_$dateSuffix"
        Rename-Item -Path $folder -NewName $newFolderName
        Write-Output "Folder $folder renamed to $newFolderName."
    } else {
        Write-Output "Folder $folder does not exist."
    }
}

In this example, I create an array of folder paths and use a foreach loop to check and rename each folder. The new folder name includes the current date as a suffix.

Example: Monthly Report Folders

Let me show you a real example. Suppose you need to rename monthly report folders. Each month, a new folder is created to store reports, and the previous month’s folder must be archived. Here’s how you can automate this process using PowerShell.

$reportFolder = "C:\Reports\CurrentMonth"
$archiveFolder = "C:\Reports\Archived"
$dateSuffix = (Get-Date).AddMonths(-1).ToString("yyyyMM")

if (Test-Path -Path $reportFolder) {
    $newFolderName = "Reports_$dateSuffix"
    Rename-Item -Path $reportFolder -NewName $newFolderName
    Move-Item -Path "$reportFolder\$newFolderName" -Destination $archiveFolder
    Write-Output "Report folder archived as $newFolderName."
} else {
    Write-Output "Current month report folder does not exist."
}

In this example, I rename the current month’s report folder with a suffix of the previous month and move it to the archive folder.

Check out How to Count Files in a Folder Using PowerShell?

PowerShell Rename Folder With Spaces

Let me show you how to rename folders containing spaces in their names.

When dealing with paths or folder names that include spaces, it’s important to handle them properly to avoid syntax errors or unintended behavior. PowerShell provides a few ways to handle spaces in folder names when renaming them.

Let’s consider an example where I have a folder named “Monthly Reports” in the “D:\Projects” directory, and I want to rename it to “Financial Reports”. Here are a few approaches to renaming a folder with spaces using PowerShell:

Approach 1: Using Single Quotes

One way to handle spaces in folder names is to enclose the path or folder name in single quotes. This tells PowerShell to treat the content within the quotes as a single entity.

Rename-Item -Path 'D:\Projects\Monthly Reports' -NewName 'Financial Reports'

In this example, both the -Path and -NewName parameters are enclosed in single quotes, allowing PowerShell to interpret the spaces correctly.

Approach 2: Using Double Quotes with Escaping

Another approach is to use double quotes and escape the spaces using the backtick character (`) followed by a space.

Rename-Item -Path "D:\Projects\Monthly` Reports" -NewName "Financial` Reports"

Here, the backtick character before each space tells PowerShell to treat the space as a literal character instead of a delimiter.

Check out How to Get Size of Folder in PowerShell

Approach 3: Using the -LiteralPath Parameter

PowerShell provides the -LiteralPath parameter, which treats the path literally, including any spaces, without the need for additional quoting or escaping.

Rename-Item -LiteralPath "D:\Projects\Monthly Reports" -NewName "Financial Reports"

In this case, we use -LiteralPath instead of -Path, and PowerShell interprets the path exactly as provided, spaces included.

It’s important to note that when using the -NewName parameter, you only need to handle spaces if the new name itself contains spaces. If the new name doesn’t have spaces, you can provide it without any special handling.

Here’s an example that demonstrates renaming a folder with spaces to a new name without spaces:

Rename-Item -Path 'D:\Projects\Monthly Reports' -NewName 'FinancialReports'

In this case, the -Path parameter is enclosed in single quotes to handle the spaces, but the -NewName parameter doesn’t require any special treatment since “FinancialReports” doesn’t contain spaces.

When renaming folders with spaces, it’s generally a good practice to use either single quotes or the -LiteralPath parameter to ensure proper handling of the spaces. This helps avoid any ambiguity or potential issues caused by spaces in the path or folder name.

Conclusion

In this tutorial, I explained how to rename a folder if it exists using PowerShell with examples. I have also explained how to rename multiple folders using PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.