-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreate-Shortcut.ps1
More file actions
130 lines (112 loc) · 4.18 KB
/
Create-Shortcut.ps1
File metadata and controls
130 lines (112 loc) · 4.18 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
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
<#
.SYNOPSIS
Creates a program shortcut at the specified location.
.DESCRIPTION
Given a CreationPath and a TargetPath, the Create-Shortcut function shall
create a shortcut pointing to TargetPath at CreationPath. In addition to
the CreationPath and TargetPath, it should also be able to handle TargetArgs,
which will be passed to the program at TargetPath as command-line parameters.
.PARAMETER CreationPath
[Required, String]
Specifies the location at which the new shortcut should be created.
If the CreationPath does not exist, then throw a System.IO.IOException.
.PARAMETER ShortcutName
[Required, String]
Name of the new shortcut.
.PARAMETER ExtIsDotUrl
[Optional, Switch]
If enabled, then append ".url" to ShortcutName. Otherwise, append ".lnk"
to ShortcutName.
.PARAMETER TargetPath
[Required, String]
Specifies location/program that this new shortcut will point to.
.PARAMETER TargetArgs
[Optional, String]
Specifies arguments to program that the new shortcut will point to.
.EXAMPLE
CreateShortcut.ps1 -CreationPath "C:\Users\abcd\Desktop"
-ShortcutName "Target"
-TargetPath "C:\Users\abcd\Documents\Test.docx"
--> Creates a shortcut called Target.lnk on user abcd's desktop; Target.lnk
shall point to a document called Test.docx, so clicking on Target should open
up this document.
CreateShortcut.ps1 -CreationPath "C:\Users\abcd\Desktop"
-ShortcutName "Target"
-ExtIsDotUrl
-TargetPath "$PsHome\powershell.exe"
-TargetArgs '-noexit -command "echo Hello World"'
--> Creates a shortcut called Target.url on user abcd's desktop; when the user
clicks on Target.lnk, it shall open up a PowerShell window, execute the command
"echo Hello World" (which will print Hello World) to the console, and leave the
window open.
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param(
[Parameter(Mandatory=$True)]
[string]
[ValidateScript({
if (!(Test-Path $_)) {
throw [System.IO.IOException] "`"$_`" is an invalid location."
}
else {
return $True
}
})]
$CreationPath,
[Parameter(Mandatory=$True)]
[string]
$ShortcutName,
[switch]
$ExtIsDotUrl,
[Parameter(Mandatory=$True)]
[ValidateScript({
if (!(Test-Path $_)) {
throw [System.IO.IOException] "`"$_`" is an invalid location."
}
else {
return $True
}
})]
[string]
$TargetPath,
[string]
$TargetArgs
)
[string]$Ext = ".lnk"
if ($ExtIsDotUrl) {
$Ext = ".url"
}
<#
.SYNOPSIS
Return the absolute path of the location where the shortcut will be created.
.DESCRIPTION
Account for the following:
1. User may have added a "\" at the end of CreationPath.
2. User may have added ".url" or ".lnk" extension to ShortcutName.
Then, concatenate the reformatted CreationPath, ShortcutName, and Ext.
#>
function GetLocationOfNewShortcut
{
# If the name already ends in ".lnk" or ".url" (shortcut extensions),
# then remove the extension since we're going to be appending it anyway.
[int]$lastDotIndex = $ShortcutName.LastIndexOf(".")
if ($lastDotIndex -ne -1) {
[string]$currentExt = $ShortcutName.Substring($lastDotIndex)
if (($currentExt -eq ".lnk") -or ($currentExt -eq ".url")) {
$ShortcutName = $ShortcutName.Substring(0, $lastDotIndex)
}
}
# Remove trailing "\" from CreationPath.
$CreationPath = $CreationPath.Trim("\");
return $CreationPath + "\" + $ShortcutName + $Ext
}
# Create the shortcut according to the specified config params.
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($(GetLocationOfNewShortcut))
$Shortcut.TargetPath = $TargetPath # New shortcut will point to $TargetPath.
if ($TargetArgs) {
# User specified that program at $TargetPath should be invoked with the
# arguments $TargetArgs.
$Shortcut.Arguments = $TargetArgs
}
$Shortcut.Save()