-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartmenu.ps1
234 lines (177 loc) · 6.26 KB
/
startmenu.ps1
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
. "$PSScriptRoot\shortcut.ps1"
$wshShell = New-Object -ComObject WScript.Shell
$allUsersProgramsPath = $wshShell.SpecialFolders("AllUsersPrograms")
# https://www.vbsedit.com/html/a239a3ac-e51c-4e70-859e-d2d8c2eb3135.asp
# $windowStyleDefault = 1
$windowStyleMaximized = 3
$windowStyleMinimized = 7
function Get-StartMenuProgramsPath {
[CmdletBinding()]
param ()
return $allUsersProgramsPath
<#
.SYNOPSIS
Returns the path to Start Menu > Programs.
.DESCRIPTION
Returns the path to the All Users Start Menu Programs folder.
.OUTPUTS
string - Path to the All Users Start Menu Programs folder.
#>
}
function New-StartMenuProgramsFolder {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[Alias("AppName")] # DEPRECATED!
[Alias("Folder", "Group")]
[string] $Name
)
$shortcutFolderName = "$allUsersProgramsPath\$Name"
if ($PSCmdlet.ShouldProcess($shortcutFolderName, "Create folder")) {
New-Item -ItemType Directory $shortcutFolderName -Force | Out-Null
}
return $shortcutFolderName
<#
.SYNOPSIS
Creates a new folder in Start Menu > Programs.
.DESCRIPTION
Creates a new folder in the All Users Start Menu Programs folder.
.PARAMETER Name
The name of the folder in the Start Menu > Programs folder.
.OUTPUTS
string - Path to the newly created folder in the All Users Start Menu Programs folder.
#>
}
function New-StartMenuShortcut {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $false)]
[Alias("App", "AppName")]
[string] $Name,
[Parameter(Mandatory = $false)]
[Alias("Group")]
[string] $Folder,
[Parameter(Mandatory = $true)]
[string] $Executable,
[Parameter(Mandatory = $false)]
[string] $Arguments,
[Parameter(Mandatory = $false)]
[Alias("IconLocation")]
[string] $Icon
)
# infer the shortcut name
$shortcutName = if ($Name) { $Name } else { ((Get-Item $Executable).BaseName) }
$folderName = if ($Folder) { $Folder } else { $shortcutName }
$shortcutFolder = New-StartMenuProgramsFolder -Name $folderName
$shortcutPath = "$shortcutFolder\$shortcutName.lnk"
$shortcut = $wshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $Executable
$shortcut.Arguments = $Arguments
If ($Icon) {
$shortcut.IconLocation = "$Icon,0"
}
if ($PSCmdlet.ShouldProcess($shortcutPath, "Create shortcut")) {
$shortcut.Save()
}
return $shortcutPath
<#
.SYNOPSIS
Creates a new shortcut in Start Menu > Programs.
.DESCRIPTION
Creates a new shortcut in the All Users Start Menu Programs folder.
.PARAMETER Name
The name of the application. This will be used as the name of the shortcut in the Start Menu > Programs folder.
.PARAMETER Folder
The name of the folder in the Start Menu > Programs folder. Default: $Name
.PARAMETER Executable
The path to the executable.
.PARAMETER Arguments
The arguments to pass to the executable.
.PARAMETER Icon
The path to the icon file to use for the shortcut.
.OUTPUTS
string - Path to the newly created shortcut in the All Users Start Menu Programs folder.
#>
}
function New-PowershellStartMenuShortcut {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[Alias("Script")]
[string] $Command,
[Parameter(Mandatory = $true)]
[Alias("App", "AppName")]
[string] $Name,
[Parameter(Mandatory = $false)]
[Alias("Group", "GroupName")]
[string] $Folder,
[Parameter(Mandatory = $false)]
[Alias("Administrator", "Admin", "Elevate")]
[switch] $RunAsAdministrator = $false,
[Parameter(Mandatory = $false)]
[switch] $Visible = $false,
[Parameter(Mandatory = $false)]
[switch] $Maximized = $false,
[Parameter(Mandatory = $false)]
[Alias("NoExit")]
[switch] $KeepOpen = $false,
[Parameter(Mandatory = $false)]
[Alias("IconLocation")]
[string] $Icon
)
$shortcutFolder = if ($Group) {
New-StartMenuProgramsFolder -Name $Group
}
else {
Get-StartMenuProgramsPath
}
$arguments = @()
if ($KeepOpen) {
$arguments += "-NoExit"
}
$arguments += "-Command `"$Command`""
$shortcutPath = "$shortcutFolder\$Name.lnk"
$shortcut = $wshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "pwsh"
$shortcut.Arguments = $arguments -join ' '
if (-not $Visible) {
$shortcut.WindowStyle = $windowStyleMinimized
}
if ($Maximized) {
$shortcut.WindowStyle = $windowStyleMaximized
}
If ($Icon) {
$shortcut.IconLocation = "$Icon,0"
}
if ($PSCmdlet.ShouldProcess($shortcutPath, "Create shortcut")) {
$shortcut.Save()
}
if ($RunAsAdministrator) {
Set-ShortcutRunAsAdministrator $shortcutPath
}
return $shortcutPath
<#
.SYNOPSIS
Creates a new shortcut that runs a PowerShell command in Start Menu > Programs.
.DESCRIPTION
Creates a new shortcut that runs a PowerShell command in the All Users Start Menu Programs folder.
.PARAMETER Command
The PowerShell command to run.
.PARAMETER Name
The name of the shortcut in the Start Menu > Programs folder.
.PARAMETER GroupName
The name of the group to create the shortcut in. If not specified, the shortcut will be created in the Start Menu > Programs folder.
.PARAMETER RunAsAdministrator
Whether to run the PowerShell command as an administrator.
.PARAMETER Visible
Whether to show the PowerShell window when the shortcut is run.
.PARAMETER Maximized
Whether to maximize the PowerShell window when the shortcut is run.
.PARAMETER KeepOpen
Whether to keep the PowerShell window open after the command has finished running.
.PARAMETER Icon
The path to the icon file to use for the shortcut.
.OUTPUTS
string - Path to the newly created shortcut in the All Users Start Menu Programs folder.
#>
}