forked from BrendanBard/bundy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutorun.ps1
297 lines (251 loc) · 9.16 KB
/
Autorun.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<#
List/add/remove autorun entries in start menu or registry for user running script or all users
Guy Leech, 2016
#>
<#
.SYNOPSIS
List/add/remove autorun entries, so items run at logon, in start menu or registry for user running the script or all users.
.DESCRIPTION
Can remove named entries or search entries for a string such as the executable name.
You will be prompted before anything is removed unless you specify -confirm:$false on the command line.
.PARAMETER allusers
By default the script operates on the invoking user's start menu or HKCU. Using this option changes the behaviour to operate on the all users start menu and HKLM.
To remove entries the script must be run with adequate rights.
.PARAMETER registry
By default the script operates on the start menu. Using this option changes the behaviour to operate on HKCU or HKLM depdending if the -allusers options is specified too.
.PARAMETER wow6432node
When in registry mode on 64 bit system, operate on the 32 bit entries instead of the 64 bit ones.
.PARAMETER run
The command line to run including any parameters. If there are spaces in the command then encase in double quote characters
.PARAMETER name
The name of the registry value or shortcut to be operated on.
.PARAMETER remove
Remove the entries that match the -name or -run options specified.
.PARAMETER list
Show the autoruns entries in the file system or registry. Use with -name to show a specific one or with -run to match some or all of a command line.
.PARAMETER verify
Verify the executable exists when creating a new entry and error if it does not.
.PARAMETER icon
Optional icon file and offset to be used when creating a shortcut, e.g. c:\windows\system32\shell32.dll,10
.PARAMETER description
Optional description/comment to be used when creating a shortcut
.EXAMPLE
& "C:\Scripts\Autorun.ps1" -list -registry -allusers -run evernote
Show all autorun entries in the registry for all users that contain the string "evernote"
.EXAMPLE
& "C:\Scripts\Autorun.ps1" -name "Edit Hosts File" -run "notepad.exe c:\windows\system32\drivers\etc\hosts" -description "Look at the hosts files"
Add a shortcut for the user called "Edit Hosts File" which will run the given command line
.EXAMPLE
& "C:\Scripts\Autorun.ps1" -name "Edit Hosts File" -remove
Remove the shortcut for the user called "Edit Hosts File"
.NOTES
Will work when start menus are redirected.
#>
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
Param
(
[switch]$allusers ,
[switch]$registry ,
[switch]$wow6432node ,
[string]$run ,
[switch]$remove ,
[switch]$list ,
[string]$name ,
[string]$icon ,
[string]$description ,
[switch]$verify
)
[int]$count = 0
if( $registry )
{
$regKey = '\Microsoft\Windows\CurrentVersion\Run'
if( $wow6432node )
{
$regKey = '\Software\Wow6432Node' + $regkey
}
else
{
$regKey = '\Software' + $regkey
}
if( $allusers )
{
$regKey = 'HKLM:' + $regKey
}
else
{
$regKey = 'HKCU:' + $regKey
}
if( $remove -or $list )
{
## if we have the name then remove that value else find values containing the run command
if( ! [string]::IsNullOrEmpty( $name ) )
{
if( $list )
{
Write-Host "$regKey : $name"
}
elseif( $pscmdlet.ShouldProcess( "Key `"$regKey`"" , "Remove `"$name`"" ) )
{
Remove-ItemProperty -Path $regKey -Name $name
if( $? )
{
$count++
}
}
}
elseif( $list -or ! [string]::IsNullOrEmpty( $run ) ) ## no name so search for command
{
Get-Item -Path $regKey | Select-Object -ExpandProperty property | %{
[string]$value = (Get-ItemProperty -Path $regKey -Name $_).$_
if( $value -match $run )
{
if( $list )
{
Write-Host "$regKey : $_ : $value"
}
elseif( $pscmdlet.ShouldProcess( "Value `"$_`"" , "Remove `"$value`"" ) )
{
Remove-ItemProperty -Path $regKey -Name $_
if( $? )
{
$count++
}
}
}
}
}
else
{
Write-Error "Must specify -name or -run when removing an entry"
return
}
}
else ## adding
{
if( [string]::IsNullOrEmpty( $name ) )
{
Write-Error "Must specify the name of the registry value via the -name option"
return
}
## See if it exists already
[string]$existing = (Get-ItemProperty -Path $regKey -Name $name -ErrorAction SilentlyContinue).$name
[bool]$carryOn = $true
if( ! [string]::IsNullOrEmpty( $existing ) )
{
if( $existing -eq $run -or ! $pscmdlet.ShouldProcess( "Value `"$name`" already exists as `"$existing`"" , "Overwrite" ) )
{
$carryOn = $false
}
}
if( $carryOn )
{
if( ! $verify -or ( Get-Command $run ) )
{
Set-ItemProperty -Path $regKey -Name $name -Value $run
}
}
}
}
else ## file system not registry
{
if( $allusers )
{
$folder = [environment]::GetFolderPath('CommonStartUp')
}
else
{
$folder = [environment]::GetFolderPath('StartUp')
}
$ShellObject = New-Object -ComObject Wscript.Shell
if( $remove -or $list )
{
if( ! [string]::IsNullOrEmpty( $name ) )
{
[string]$fullPath = $folder + '\' + $name + '.lnk'
if( $pscmdlet.ShouldProcess( $fullPath , "Delete" ) )
{
Remove-Item -Path $fullPath -Force
if( $? )
{
$count++
}
}
}
elseif( $list -or ! [string]::IsNullOrEmpty( $run ) ) ## no name so search shortcut content
{
Get-ChildItem -Path $folder -Include *.lnk -Recurse | %{
$shortcut = $ShellObject.CreateShortcut($_.FullName)
[string]$targetPath = $shortcut.TargetPath
if( $targetPath -match $run )
{
if( $list )
{
Write-Host "$($_.FullName) : $targetPath $($shortcut.Arguments)"
}
elseif( $pscmdlet.ShouldProcess( "$($_.FullName)" , "Delete" ) )
{
Remove-Item -Path $_ -Force
if( $? )
{
$count++
}
}
}
}
}
else
{
Write-Error "Must specify -name or -run when removing an entry"
}
}
else ## add
{
if( [string]::IsNullOrEmpty( $name ) )
{
Write-Error "Must specify the name of the shortcut via the -name option"
return
}
[string]$fullPath = $folder + '\' + $name + '.lnk'
if( ! ( Test-Path $fullPath ) -or $pscmdlet.ShouldProcess( "`"$fullPath`" already exists" , "Overwrite" ) )
{
## Shortcut needs exe (TargetPath) separate to the arguments so we must split out
## If first character is quote then find matching one else we break on whitespace
[string]$target = $run
[string]$arguments = $null
if( $run.StartsWith( "`"" ) )
{
$end = $run.Substring( 1 ).IndexOf("`"")
$target = $run.Substring( 1 , $end )
$arguments = $run.Substring($end + 2).TrimStart() ## skip passed " and index is zero based
}
elseif( $run.IndexOf( ' ' ) -ge 0 )
{
$target = $run.Substring( 0 , $run.IndexOf( ' ' ) )
$arguments = $run.Substring( $run.IndexOf(' ' ) ).TrimStart()
}
[string]$fullPath = $folder + '\' + $name + '.lnk'
$shortcut = $ShellObject.CreateShortcut($fullPath)
$shortcut.TargetPath = $target
if( ! [string]::IsNullOrEmpty( $arguments ) )
{
$shortcut.Arguments = $arguments
}
if( ! [string]::IsNullOrEmpty( $icon ) )
{
$shortcut.IconLocation = $icon
}
if( ! [string]::IsNullOrEmpty( $description ) )
{
$shortcut.Description = $description
}
if( ! $verify -or ( Get-Command $target ) )
{
$shortcut.Save()
}
}
}
}
if( $remove -and ! $count )
{
Write-Warning "Removed no items"
}