Skip to content

Instantly share code, notes, and snippets.

@valorad
Created October 31, 2020 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valorad/6aa7f4a1d4136a346f4d1b9738342dc0 to your computer and use it in GitHub Desktop.
Save valorad/6aa7f4a1d4136a346f4d1b9738342dc0 to your computer and use it in GitHub Desktop.
Link Windows user directory folders to somewhere else
Write-Host "==============================================================="
Write-Host "=========================== Link On ==========================="
Write-Host "==============================================================="
# Configs -->
$linkConfig = [ordered]@{
"originFolder1" = "D:\test\targetFolder1"
"originFolder2" = "D:\test\targetFolder2"
}
# <-- Configs
# Functions -->
function Confirm-UserName($username) {
Write-Host "`n Welcome! This program assumes that you are: "
Write-Host "- Running as ** Administrator **"
Write-Host "- Using the identity of $username, with a valid user directory at C:\Users\$username `n"
}
function Confirm-OriginFolder($linkMap) {
Write-Host "`n Next, we will first DELETE everything beneath the original folder, "
Write-Host " then establish a symlink to the target directory."
Write-Host " Please confirm the links that we are going to establish. `n"
Write-Host "==============================================================="
foreach ($key in $linkMap.GetEnumerator()) {
Write-Host "$($key.Name) --> $($key.Value)"
}
Write-Host "===============================================================`n"
}
function Create-LinkMap($username) {
$linkMap = [ordered]@{}
foreach ($key in $linkConfig.GetEnumerator()) {
$linkMap.Add("C:\Users\$username\$($key.name)", $key.Value)
}
return $linkMap
}
function Create-Link ($source, $target) {
Remove-Item -Recurse -ErrorAction SilentlyContinue $source
New-Item -ItemType SymbolicLink -Path $source -Value $target
}
# <-- Functions
$currentUsername = $env:username
$isNameOkay = $false
# Name confirmation
Confirm-UserName $currentUsername
while (-NOT($isNameOkay)) {
$OKSelection = Read-Host "Is the name correct? (Y/n)"
if (($OKSelection -eq "Y") -OR ($OKSelection -eq "y")) {
$isNameOkay = $true
} else {
$currentUsername = Read-Host "Who are you? ($env:username)"
if ([string]::IsNullOrWhiteSpace($currentUsername)) {
$currentUsername = $env:username
}
Confirm-UserName $currentUsername
}
}
# Folder confirmation + begin link establishment
$linkMap = Create-LinkMap $currentUsername
Confirm-OriginFolder $linkMap
Write-Host "`n Please confirm one more time. Once you say yes, there will be NO turning back!`n"
$OKSelection = Read-Host " Folders are correct and ABSOLUTELY NO important files are stored? (Y/n)"
if (($OKSelection -eq "Y") -OR ($OKSelection -eq "y")) {
foreach ($key in $linkMap.GetEnumerator()) {
Create-Link $key.Name $key.Value
}
} else {
Write-Host "Operation has been aborted."
exit -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment