-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnePowerShellProfile.psm1
143 lines (120 loc) · 5.55 KB
/
OnePowerShellProfile.psm1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function Test-InstalledProgram {
$Name = $args[0]
if ($null -eq $Name -or $Name -eq '') {
Write-Error 'Parameter not found. Provide a variation of the installed program name as an argument.'
}
$localMachineKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$currentUserKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$getLocalMachineKey = Get-ItemProperty $localMachineKey | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Where-Object { $_.DisplayName -match $Name }
$getCurrentUserKey = Get-ItemProperty $currentUserKey | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Where-Object { $_.DisplayName -match $Name }
if ($null -ne $getLocalMachineKey) {
Write-Output "Found registry match in HKLM (local machine) for program '$Name'"
$getLocalMachineKey
return $true
}
elseif ($null -ne $getCurrentUserKey) {
Write-Output "Found registry match in HKCU (current user) for program '$Name'"
$getCurrentUserKey
return $true
}
else {
Write-Output "Registry match not found '$Name'." -Foreground Red
return $false
}
}
function Invoke-ProfileConfiguration {
param(
[string] $Type
)
try {
$File = ''
if ($Type -eq "Public" -or $Type -eq "Private")
{
$ModuleBase = (Get-Module OnePowerShellProfile -ListAvailable).ModuleBase
$File = Join-Path $ModuleBase "\Profile\$Type.ps1"
}
else {
throw "Parameter 'Type' is expecting profile type values of 'Public' or 'Private'"
}
if ((Test-InstalledProgram { Visual Studio Code }) -eq $true) {
Write-Output "Opening '$File' in Visual Studio Code"
code $File
}
else {
Write-Output "Opening '$File' in default text editor"
Invoke-Item $File
}
}
catch {
throw
}
}
function Set-Profile {
[CmdletBinding(SupportsShouldProcess=$True)]
param(
[boolean] $IncludePrivate = $False,
[boolean] $ArchiveOldProfile = $False
)
try {
$PSProfileRootPath = $env:USERPROFILE
# Check for valid Documents path w/ PowerShell modules
$DocumentsOneDrive = $env:USERPROFILE + '\OneDrive'
$DocumentsLocal = $env:USERPROFILE + '\Documents'
# Resolve target `Documents` path (OneDrive (if available) preceeds local)
if (!(Test-Path $DocumentsOneDrive)) {
Write-Output "[NOTICE] Using '$DocumentsOneDrive' to reference PowerShell profiles"
$PSProfileRootPath = $env:USERPROFILE + '\OneDrive - Microsoft\Documents'
}
else {
Write-Output "[NOTICE] Using '$DocumentsLocal' to reference PowerShell profiles"
$PSProfileRootPath = $env:USERPROFILE + '\Documents'
}
if ($PSCmdlet.ShouldProcess($PSProfileRootPath, "Create new directories")) {
New-Item -ItemType Directory -Name "WindowsPowerShell" -Path $PSProfileRootPath -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Name "PowerShell" -Path $PSProfileRootPath -ErrorAction SilentlyContinue
}
$ISE = $PSProfileRootPath + '\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1'
$Desktop = $PSProfileRootPath + '\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
$Core = $PSProfileRootPath + '\PowerShell\Microsoft.PowerShell_profile.ps1'
$PSProfiles = $ISE, $Desktop, $Core
foreach ($TargetProfile in $PSProfiles) {
$TargetProfilePath = Split-Path $TargetProfile
$TargetProfileFileName = Split-Path $TargetProfile -Leaf
if (!(Test-Path $TargetProfile)) {
if ($PSCmdlet.ShouldProcess($TargetProfile, "Create new file"))
{
New-Item -ItemType File -Name (Split-Path $TargetProfile -Leaf) -Path $TargetProfilePath
}
}
if ($ArchiveOldProfile -eq $True)
{
if (!(Test-Path ($TargetProfilePath + "\Archived"))) {
if ($PSCmdlet.ShouldProcess($TargetProfilePath + "\Archived", "Create new directory")) {
New-Item -ItemType Directory -Name "Archived" -Path $TargetProfilePath
}
}
else {
$Timestamp = $timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
$ArchivedFileName = $Timestamp + "_" + $TargetProfileFileName
if ($PSCmdlet.ShouldProcess($TargetProfile, "Archive file")) {
Copy-Item $TargetProfile ($TargetProfilePath + "\Archived\" + $ArchivedFileName)
}
Write-Output "Archived $ArchivedFileName at $TargetProfileFileName"
}
}
$ModuleBase = (Get-Module OnePowerShellProfile -ListAvailable).ModuleBase
$PublicProfile = Join-Path $ModuleBase "\Profile\Public.ps1"
$PrivateProfile = Join-Path $ModuleBase "\Profile\Private.ps1"
Get-Content $PublicProfile > $TargetProfile
if ($IncludePrivate -eq $true) {
Get-Content $PrivateProfile >> $TargetProfile
Write-Output "Appended $PrivateProfile to $TargetProfile"
}
Write-Output "[DONE] Updated '$TargetProfile'"
}
Write-Output "[NOTICE] Restart a new session, or invoke '. `$PROFILE' to source the latest profile in this session"
}
catch {
throw
}
}