#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Rename Folders with Special Characters

In this post, we will be renaming folders with special characters based on the spreadsheet created in the last blog post. Data contains following columns respectively:

  • ProjectID
  • Name
  • FullPath
  • URN

All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 24.0.1. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Get-TablesFromXLSXWorkbook
  • Get-PWFolders
  • Update-PWFolderNameProps

Import Data From Excel

We will run the Get-TablesFromXLSXWorkbook cmdlet to import the data exported in the previous post. This will grab the data from the first worksheet within the Excel spreadsheet.

# Import data from spreadsheet. Must be formatted appropriately.
# 'ProjectID', 'Name', 'FullPath', 'URN'
$dtTemp = (Get-TablesFromXLSXWorkbook -InputFileName C:\temp\FoldersWithSpecialCharactersInName_20250317.xlsx -Verbose)[0]

Rename Folders

Now we will loop through the data returned and update the folders replacing all special characters with an underscore.

foreach($row in $dtTemp.Rows){
    Write-Host "'$($row.Name)' folder to be renamed." -ForegroundColor Cyan
    try {
        if($pwFolder_Temp = Get-PWFolders -FolderID $row.ProjectID -JustOne -Verbose){
            $name = $pwFolder_Temp.Name

            #region IF STATEMENTS

            if($name.Contains('<')){
                $name = $name.Replace('<', '_')
            }
            if($name.Contains('>')){
                $name = $name.Replace('>', '_')
            }
            if($name.Contains(':')){
                $name = $name.Replace(':', '_')
            }
            if($name.Contains('/')){
                $name = $name.Replace('/', '_')
            }
            if($name.Contains('\')){
                $name = $name.Replace('\', '_')
            }
            if($name.Contains('|')){
                $name = $name.Replace('|', '_')
            }
            if($name.Contains('?')){
                $name = $name.Replace('?', '_')
            }
            if($name.Contains('*')){
                $name = $name.Replace('*', '_')
            }
            if($name.Contains('"')){
                $name = $name.Replace('"', '_')
            }

            #endregion IF STATEMENTS

            if($return = Update-PWFolderNameProps -InputFolder $pwFolder_Temp -NewName $name -Verbose){
                Write-Host "Successfully renamed folder from '$($row.Name)' to '$name'." -ForegroundColor Green
            } else {
                throw "Failed to rename folder '$($row.Name)'."
            }

        } else {
            throw "Failed to get '$($row.Name)' folder."
        }

    } catch {
        Write-Warning -Message $_
    }
} # end foreach($row in $dtTemp.Rows)...


Experiment with it and have fun.

Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.