Skip to content

Instantly share code, notes, and snippets.

@Citillara
Created August 4, 2025 12:31
Show Gist options
  • Save Citillara/4f907f0b129e92e2afd206b43cdb0639 to your computer and use it in GitHub Desktop.
Save Citillara/4f907f0b129e92e2afd206b43cdb0639 to your computer and use it in GitHub Desktop.
param(
[Parameter(Mandatory = $true)]
[string]$FolderName, # e.g. "Assassin's Creed Odyssey"
[switch]$Merge # Allow merging into a non-empty destination
)
# Usage : .\Move-ToMyGamesJunction.ps1 -FolderName "Assassin's Creed Odyssey"
# Merge if exist : .\Move-ToMyGamesJunction.ps1 -FolderName "Assassin's Creed Odyssey" -Merge
$docs = [Environment]::GetFolderPath('MyDocuments')
$src = Join-Path $docs $FolderName
$myGames = Join-Path $docs 'My Games'
$dst = Join-Path $myGames $FolderName
Write-Host "Source: $src"
Write-Host "Destination: $dst"
Write-Host ""
# 1) Sanity checks
if (-not (Test-Path -LiteralPath $src)) {
Write-Error "Source '$src' not found."
exit 1
}
$srcItem = Get-Item -LiteralPath $src -Force
if (($srcItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
Write-Error "Source '$src' is already a reparse point (likely a junction)."
exit 1
}
if (-not (Test-Path -LiteralPath $myGames)) {
New-Item -ItemType Directory -Path $myGames | Out-Null
}
$dstExists = Test-Path -LiteralPath $dst
if (-not $dstExists) {
New-Item -ItemType Directory -Path $dst | Out-Null
} else {
if (-not $Merge) {
$count = (Get-ChildItem -LiteralPath $dst -Force | Measure-Object).Count
if ($count -gt 0) {
Write-Error "Destination '$dst' exists and is not empty. Use -Merge to merge."
exit 1
}
}
}
# 2) Move contents from Documents\<FolderName> -> Documents\My Games\<FolderName>
Write-Host "Moving contents..."
try {
Get-ChildItem -LiteralPath $src -Force -ErrorAction Stop | ForEach-Object {
Move-Item -LiteralPath $_.FullName -Destination $dst -Force -ErrorAction Stop
}
} catch {
Write-Error "Move failed: $($_.Exception.Message)"
exit 1
}
# 3) Remove now-empty source folder (so we can place a junction with the same name)
try {
Remove-Item -LiteralPath $src -Force -ErrorAction Stop
} catch {
try {
# In case of stubborn empty dirs/attributes
Remove-Item -LiteralPath $src -Recurse -Force -ErrorAction Stop
} catch {
Write-Error "Could not remove the original folder to create a junction: $($_.Exception.Message)"
exit 1
}
}
# 4) Create junction at old location pointing to new location
try {
New-Item -ItemType Junction -Path $src -Target $dst | Out-Null
} catch {
Write-Error "Failed to create junction: $($_.Exception.Message)"
exit 1
}
# 5) Hide the junction so Documents stays tidy
try {
$junction = Get-Item -LiteralPath $src -Force
$junction.Attributes = $junction.Attributes -bor [IO.FileAttributes]::Hidden
} catch {
Write-Warning "Junction created but could not set Hidden attribute: $($_.Exception.Message)"
}
Write-Host "Done."
Write-Host "Moved to: $dst"
Write-Host "Hidden junction at: $src"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment