Skip to content

$cs.File_RenameDir

Renames or moves a directory. Throws DirectoryNotFoundException if source doesn’t exist. If dest does not already exist, this is a plain fast move. If dest already exists, the behavior depends on overwrite: $true merges by copying the contents of source into dest (the same as calling $cs.File_CopyTree) and then deletes source (the same as calling $cs.File_DelTree) — this is a copy-then-delete, not an atomic rename; $false does nothing and returns without error.

$cs.File_RenameDir(string source, string dest, bool overwrite = true)

Full path of the directory to rename or move. Must exist.

Full path of the destination directory.

If dest already exists: $true merges source into dest by copying then deleting source; $false leaves both directories untouched.

None.

Terminal window
$oldFolder = Join-Path $global:Packageroot "Temp"
$newFolder = Join-Path $global:Packageroot "Temp2"
$cs.File_RenameDir($oldFolder, $newFolder, $false)

Archive an old versioned install folder before laying down a fresh one, so a prior install is never lost:

Terminal window
$currentInstall = Join-Path ${env:ProgramFiles(x86)} "Contoso\AppV1"
$backupFolder = Join-Path ${env:ProgramFiles(x86)} "Contoso\AppV1_backup"
if ($cs.File_ExistDir($currentInstall))
{
$cs.Job_WriteLog("Archiving existing install to $backupFolder")
$cs.File_RenameDir($currentInstall, $backupFolder, $true)
}

Merge a newly extracted profile template into an existing user data folder, overwriting any conflicting files:

Terminal window
$stagingFolder = Join-Path $global:Packageroot "ProfileTemplate"
$targetFolder = Join-Path $env:ProgramData "Contoso\ProfileTemplate"
$cs.File_RenameDir($stagingFolder, $targetFolder, $true)

$cs.File_RenameFile $cs.File_CopyTree $cs.File_DelTree $cs.File_ExistDir