-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit-win.ps1
More file actions
48 lines (45 loc) · 1.64 KB
/
init-win.ps1
File metadata and controls
48 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$isadmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
$profpath = "$PSScriptRoot\PowerShell\profile.ps1"
$defaultprofpath = $PROFILE.CurrentUserAllHosts
$backup = $false
$createlink = $false
if (Test-Path $defaultprofpath) {
$defaultprof = (Get-Item $defaultprofpath)
if (<# 1. is not symlink #> `
![bool]$defaultprof.Target `
<# 2. points to invalid path #> `
-or !(Test-Path $defaultprof.Target) `
<# 3. does not points to correct path #> `
-or ((Resolve-Path $defaultprof.Target).Path `
-cne (Resolve-Path $profpath).Path) `
) {
$backup = $true
$createlink = $true
} else {
# do nothing
}
} else {
# profile.ps1 does not exist
$createlink = $true
}
if ($createlink) {
if ($isadmin){
try {
if ($backup) {
# back-ups current profile.ps1
Move-Item -Path $defaultprof -Destination "$defaultprof.bak"
}
$sl = New-Item -ItemType SymbolicLink `
-Path $defaultprofpath `
-Value $profpath
Write-Host "Succeed: created symlink($($sl.FullName) -> $($sl.Target))"
}
catch {
Write-Host "Error: cannot create symlink(backing-up failed)"
}
} else {
Write-Host "Error: cannot create symlink(must be run as an administrator)"
}
} else {
Write-Host "Note: no operations required"
}