-
Notifications
You must be signed in to change notification settings - Fork 173
/
Windows_VDOT.ps1
755 lines (697 loc) · 46.7 KB
/
Windows_VDOT.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<#####################################################################################################################################
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant
You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form
of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in
which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code
is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits,
including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
Microsoft provides programming examples for illustration only, without warranty either expressed or
implied, including, but not limited to, the implied warranties of merchantability and/or fitness
for a particular purpose.
This sample assumes that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can help explain the
functionality of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. if you have limited programming
experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting
line at (800) 936-5200.
For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:
https://partner.microsoft.com/global/30000104
######################################################################################################################################>
[Cmdletbinding(DefaultParameterSetName="Default")]
Param (
# Parameter help description
[ArgumentCompleter( { Get-ChildItem $PSScriptRoot -Directory | Select-Object -ExpandProperty Name } )]
[System.String]$WindowsVersion = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\").ReleaseId,
[ValidateSet('All','WindowsMediaPlayer','AppxPackages','ScheduledTasks','DefaultUserSettings','LocalPolicy','Autologgers','Services','NetworkOptimizations','DiskCleanup')]
[String[]]
$Optimizations,
[Parameter()]
[ValidateSet('All', 'Edge', 'RemoveLegacyIE', 'RemoveOneDrive')]
[String[]]
$AdvancedOptimizations,
[Switch]$Restart,
[Switch]$AcceptEULA
)
#Requires -RunAsAdministrator
#Requires -PSEdition Desktop
<#
- TITLE: Microsoft Windows Virtual Desktop Optimization Script
- AUTHORED BY: Robert M. Smith and Tim Muessig (Microsoft)
- AUTHORED DATE: 11/19/2019
- CONTRIBUTORS: Travis Roberts (2020), Jason Parker (2020), @brentil (2024)
- LAST UPDATED: 6/14/2024
- PURPOSE: To automatically apply many optimization settings to and Windows device; VDI, AVD, standalone machine
- Important: Every setting in this script and input files are possible optimizations only,
and NOT recommendations or requirements. Please evaluate every setting for applicability
to your specific environment. These scripts have been tested on Hyper-V VMs, as well as Azure VMs...
including Windows 11 23H2.
Please test thoroughly in your environment before implementation
- DEPENDENCIES 1. On the target machine, run PowerShell elevated (as administrator)
2. Within PowerShell, set exectuion policy to enable the running of scripts.
Ex. Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
5. This PowerShell script
6. The text input files containing all the apps, services, traces, etc. that you...
may be interested in disabling. Please review these input files to customize...
to your environment/requirements
- REFERENCES:
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-6
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-service?view=powershell-6
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-6
https://msdn.microsoft.com/en-us/library/cc422938.aspx
#>
<# Categories of cleanup items:
The UWP app input file contains the list of almost all the UWP application packages that can be removed with PowerShell interactively.
The Store and a few others, such as Wallet, were left off intentionally. Though it is possible to remove the Store app,
it is nearly impossible to get it back. Please review the configuration files and change the 'VDIState' to anything but 'disabled' to keep the item.
#>
BEGIN
{
[Version]$VDOTVersion = "2.1.2009.1"
# Create Key
$KeyPath = 'HKLM:\SOFTWARE\VDOT'
If (-Not(Test-Path $KeyPath))
{
New-Item -Path $KeyPath | Out-Null
}
# Add VDOT Version Key
$Version = "Version"
$VersionValue = $VDOTVersion
If (Get-ItemProperty $KeyPath -Name Version -ErrorAction SilentlyContinue)
{
Set-ItemProperty -Path $KeyPath -Name $Version -Value $VersionValue
}
Else
{
New-ItemProperty -Path $KeyPath -Name $Version -Value $VersionValue | Out-Null
}
# Add VDOT Last Run
$LastRun = "LastRunTime"
$LastRunValue = Get-Date
If (Get-ItemProperty $KeyPath -Name LastRunTime -ErrorAction SilentlyContinue)
{
Set-ItemProperty -Path $KeyPath -Name $LastRun -Value $LastRunValue
}
Else
{
New-ItemProperty -Path $KeyPath -Name $LastRun -Value $LastRunValue | Out-Null
}
$EventSources = @('VDOT', 'WindowsMediaPlayer', 'AppxPackages', 'ScheduledTasks', 'DefaultUserSettings', 'Autologgers', 'Services', 'LocalPolicy', 'NetworkOptimizations', 'AdvancedOptimizations', 'DiskCleanup')
If (-not([System.Diagnostics.EventLog]::SourceExists("Virtual Desktop Optimization")))
{
# All VDOT main function Event ID's [1-9]
New-EventLog -Source $EventSources -LogName 'Virtual Desktop Optimization'
Limit-EventLog -OverflowAction OverWriteAsNeeded -MaximumSize 64KB -LogName 'Virtual Desktop Optimization'
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "Log Created"
}
Else
{
New-EventLog -Source $EventSources -LogName 'Virtual Desktop Optimization' -ErrorAction SilentlyContinue
}
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "Starting VDOT by user '$env:USERNAME', for VDOT build '$WindowsVersion', with the following options:`n$($PSBoundParameters | Out-String)"
$StartTime = Get-Date
$CurrentLocation = Get-Location
$WorkingLocation = (Join-Path $PSScriptRoot $WindowsVersion)
try
{
Push-Location (Join-Path $PSScriptRoot $WindowsVersion)-ErrorAction Stop
}
catch
{
$Message = "Invalid Path $WorkingLocation - Exiting Script!"
Write-EventLog -Message $Message -Source 'VDOT' -EventID 100 -EntryType Error -LogName 'Virtual Desktop Optimization'
Write-Warning $Message
Return
}
}
PROCESS {
if (-not ($PSBoundParameters.Keys -match 'Optimizations') )
{
Write-EventLog -Message "No Optimizations (Optimizations or AdvancedOptimizations) passed, exiting script!" -Source 'VDOT' -EventID 100 -EntryType Error -LogName 'Virtual Desktop Optimization'
$Message = "`nThe Optimizations parameter no longer defaults to 'All', you must explicitly pass in this parameter.`nThis is to allow for running 'AdvancedOptimizations' separately "
Write-Host " * " -ForegroundColor black -BackgroundColor yellow -NoNewline
Write-Host " Important " -ForegroundColor Yellow -BackgroundColor Red -NoNewline
Write-Host " * " -ForegroundColor black -BackgroundColor yellow -NoNewline
Write-Host $Message -ForegroundColor yellow -BackgroundColor black
Return
}
$EULA = Get-Content ..\EULA.txt
If (-not($AcceptEULA))
{
$Title = "Accept EULA"
$Message = ""
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$EULA
$Response = $host.UI.PromptForChoice($Title, $Message, $Options, 0)
If ($Response -eq 0)
{
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "EULA Accepted"
}
Else
{
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Warning -EventId 1 -Message "EULA Declined, exiting!"
Set-Location $CurrentLocation
$EndTime = Get-Date
$ScriptRunTime = New-TimeSpan -Start $StartTime -End $EndTime
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "VDOT Total Run Time: $($ScriptRunTime.Hours) Hours $($ScriptRunTime.Minutes) Minutes $($ScriptRunTime.Seconds) Seconds"
Write-Host "`n`nThank you from the Virtual Desktop Optimization Team" -ForegroundColor Cyan
continue
}
}
Else
{
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "EULA Accepted by Parameter"
}
#region Disable, then remove, Windows Media Player including payload
If ($Optimizations -contains "WindowsMediaPlayer" -or $Optimizations -contains "All") {
try
{
Write-EventLog -EventId 10 -Message "[VDI Optimize] Disable / Remove Windows Media Player" -LogName 'Virtual Desktop Optimization' -Source 'WindowsMediaPlayer' -EntryType Information
Write-Host "[VDI Optimize] Disable / Remove Windows Media Player" -ForegroundColor Cyan
Disable-WindowsOptionalFeature -Online -FeatureName WindowsMediaPlayer -NoRestart | Out-Null
Get-WindowsPackage -Online -PackageName "*Windows-mediaplayer*" | ForEach-Object {
Write-EventLog -EventId 10 -Message "Removing $($_.PackageName)" -LogName 'Virtual Desktop Optimization' -Source 'WindowsMediaPlayer' -EntryType Information
Remove-WindowsPackage -PackageName $_.PackageName -Online -ErrorAction SilentlyContinue -NoRestart | Out-Null
}
}
catch
{
Write-EventLog -EventId 110 -Message "Disabling / Removing Windows Media Player - $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'WindowsMediaPlayer' -EntryType Error
}
}
#endregion
#region Begin Clean APPX Packages
If ($Optimizations -contains "AppxPackages" -or $Optimizations -contains "All")
{
$AppxConfigFilePath = ".\ConfigurationFiles\AppxPackages.json"
If (Test-Path $AppxConfigFilePath)
{
Write-EventLog -EventId 20 -Message "[VDI Optimize] Removing Appx Packages" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Information
Write-Host "[VDI Optimize] Removing Appx Packages" -ForegroundColor Cyan
$AppxPackage = (Get-Content $AppxConfigFilePath | ConvertFrom-Json).Where( { $_.VDIState -eq 'Disabled' })
If ($AppxPackage.Count -gt 0)
{
Foreach ($Item in $AppxPackage)
{
try
{
Write-EventLog -EventId 20 -Message "Removing Provisioned Package $($Item.AppxPackage)" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Information
Write-Verbose "Removing Provisioned Package $($Item.AppxPackage)"
Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -like ("*{0}*" -f $Item.AppxPackage) } | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue | Out-Null
Write-EventLog -EventId 20 -Message "Attempting to remove [All Users] $($Item.AppxPackage) - $($Item.Description)" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Information
Write-Verbose "Attempting to remove [All Users] $($Item.AppxPackage) - $($Item.Description)"
Get-AppxPackage -AllUsers -Name ("*{0}*" -f $Item.AppxPackage) | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue
Write-EventLog -EventId 20 -Message "Attempting to remove $($Item.AppxPackage) - $($Item.Description)" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Information
Write-Verbose "Attempting to remove $($Item.AppxPackage) - $($Item.Description)"
Get-AppxPackage -Name ("*{0}*" -f $Item.AppxPackage) | Remove-AppxPackage -ErrorAction SilentlyContinue | Out-Null
}
catch
{
Write-EventLog -EventId 120 -Message "Failed to remove Appx Package $($Item.AppxPackage) - $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Error
Write-Warning "Failed to remove Appx Package $($Item.AppxPackage) - $($_.Exception.Message)"
}
}
}
Else
{
Write-EventLog -EventId 20 -Message "No AppxPackages found to disable" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Warning
Write-Warning "No AppxPackages found to disable in $AppxConfigFilePath"
}
}
Else
{
Write-EventLog -EventId 20 -Message "Configuration file not found - $AppxConfigFilePath" -LogName 'Virtual Desktop Optimization' -Source 'AppxPackages' -EntryType Warning
Write-Warning "Configuration file not found - $AppxConfigFilePath"
}
}
#endregion
#region Disable Scheduled Tasks
# This section is for disabling scheduled tasks. If you find a task that should not be disabled
# change its "VDIState" from Disabled to Enabled, or remove it from the json completely.
If ($Optimizations -contains 'ScheduledTasks' -or $Optimizations -contains "All") {
$ScheduledTasksFilePath = ".\ConfigurationFiles\ScheduledTasks.json"
If (Test-Path $ScheduledTasksFilePath)
{
Write-EventLog -EventId 30 -Message "[VDI Optimize] Disable Scheduled Tasks" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Information
Write-Host "[VDI Optimize] Disable Scheduled Tasks" -ForegroundColor Cyan
$SchTasksList = (Get-Content $ScheduledTasksFilePath | ConvertFrom-Json).Where( { $_.VDIState -eq 'Disabled' })
If ($SchTasksList.count -gt 0)
{
Foreach ($Item in $SchTasksList)
{
$TaskObject = Get-ScheduledTask $Item.ScheduledTask
If ($TaskObject -and $TaskObject.State -ne 'Disabled')
{
Write-EventLog -EventId 30 -Message "Attempting to disable Scheduled Task: $($TaskObject.TaskName)" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Information
Write-Verbose "Attempting to disable Scheduled Task: $($TaskObject.TaskName)"
try
{
Disable-ScheduledTask -InputObject $TaskObject | Out-Null
Write-EventLog -EventId 30 -Message "Disabled Scheduled Task: $($TaskObject.TaskName)" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Information
}
catch
{
Write-EventLog -EventId 130 -Message "Failed to disabled Scheduled Task: $($TaskObject.TaskName) - $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Error
}
}
ElseIf ($TaskObject -and $TaskObject.State -eq 'Disabled')
{
Write-EventLog -EventId 30 -Message "$($TaskObject.TaskName) Scheduled Task is already disabled - $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Warning
}
Else
{
Write-EventLog -EventId 130 -Message "Unable to find Scheduled Task: $($TaskObject.TaskName) - $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Error
}
}
}
Else
{
Write-EventLog -EventId 30 -Message "No Scheduled Tasks found to disable" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Warning
}
}
Else
{
Write-EventLog -EventId 30 -Message "File not found! - $ScheduledTasksFilePath" -LogName 'Virtual Desktop Optimization' -Source 'ScheduledTasks' -EntryType Warning
}
}
#endregion
#region Customize Default User Profile
# Apply appearance customizations to default user registry hive, then close hive file
If ($Optimizations -contains "DefaultUserSettings" -or $Optimizations -contains "All")
{
$DefaultUserSettingsFilePath = ".\ConfigurationFiles\DefaultUserSettings.json"
If (Test-Path $DefaultUserSettingsFilePath)
{
Write-EventLog -EventId 40 -Message "Set Default User Settings" -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information
Write-Host "[VDI Optimize] Set Default User Settings" -ForegroundColor Cyan
$UserSettings = (Get-Content $DefaultUserSettingsFilePath | ConvertFrom-Json).Where( { $_.SetProperty -eq $true })
If ($UserSettings.Count -gt 0)
{
Write-EventLog -EventId 40 -Message "Processing Default User Settings (Registry Keys)" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
Write-Verbose "Processing Default User Settings (Registry Keys)"
$null = Start-Process reg -ArgumentList "LOAD HKLM\VDOT_TEMP C:\Users\Default\NTUSER.DAT" -PassThru -Wait
# & REG LOAD HKLM\VDOT_TEMP C:\Users\Default\NTUSER.DAT | Out-Null
Foreach ($Item in $UserSettings)
{
If ($Item.PropertyType -eq "BINARY")
{
$Value = [byte[]]($Item.PropertyValue.Split(","))
}
Else
{
$Value = $Item.PropertyValue
}
If (Test-Path -Path ("{0}" -f $Item.HivePath))
{
Write-EventLog -EventId 40 -Message "Found $($Item.HivePath) - $($Item.KeyName)" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
Write-Verbose "Found $($Item.HivePath) - $($Item.KeyName)"
If (Get-ItemProperty -Path ("{0}" -f $Item.HivePath) -ErrorAction SilentlyContinue)
{
Write-EventLog -EventId 40 -Message "Set $($Item.HivePath) - $Value" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
Set-ItemProperty -Path ("{0}" -f $Item.HivePath) -Name $Item.KeyName -Value $Value -Type $Item.PropertyType -Force
}
Else
{
Write-EventLog -EventId 40 -Message "New $($Item.HivePath) Name $($Item.KeyName) PropertyType $($Item.PropertyType) Value $Value" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
New-ItemProperty -Path ("{0}" -f $Item.HivePath) -Name $Item.KeyName -PropertyType $Item.PropertyType -Value $Value -Force | Out-Null
}
}
Else
{
Write-EventLog -EventId 40 -Message "Registry Path not found $($Item.HivePath)" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
Write-EventLog -EventId 40 -Message "Creating new Registry Key $($Item.HivePath)" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Information
$newKey = New-Item -Path ("{0}" -f $Item.HivePath) -Force
If (Test-Path -Path $newKey.PSPath)
{
New-ItemProperty -Path ("{0}" -f $Item.HivePath) -Name $Item.KeyName -PropertyType $Item.PropertyType -Value $Value -Force | Out-Null
}
Else
{
Write-EventLog -EventId 140 -Message "Failed to create new Registry Key" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Error
}
}
}
$null = Start-Process reg -ArgumentList "UNLOAD HKLM\VDOT_TEMP" -PassThru -Wait
# & REG UNLOAD HKLM\VDOT_TEMP | Out-Null
}
Else
{
Write-EventLog -EventId 40 -Message "No Default User Settings to set" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Warning
}
}
Else
{
Write-EventLog -EventId 40 -Message "File not found: $DefaultUserSettingsFilePath" -LogName 'Virtual Desktop Optimization' -Source 'DefaultUserSettings' -EntryType Warning
} }
#endregion
#region Disable Windows Traces
If ($Optimizations -contains "AutoLoggers" -or $Optimizations -contains "All")
{
$AutoLoggersFilePath = ".\ConfigurationFiles\Autologgers.Json"
If (Test-Path $AutoLoggersFilePath)
{
Write-EventLog -EventId 50 -Message "Disable AutoLoggers" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Information
Write-Host "[VDI Optimize] Disable Autologgers" -ForegroundColor Cyan
$DisableAutologgers = (Get-Content $AutoLoggersFilePath | ConvertFrom-Json).Where( { $_.Disabled -eq 'True' })
If ($DisableAutologgers.count -gt 0)
{
Write-EventLog -EventId 50 -Message "Disable AutoLoggers" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Information
Write-Verbose "Processing Autologger Configuration File"
Foreach ($Item in $DisableAutologgers)
{
Write-EventLog -EventId 50 -Message "Updating Registry Key for: $($Item.KeyName)" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Information
Write-Verbose "Updating Registry Key for: $($Item.KeyName)"
Try
{
New-ItemProperty -Path ("{0}" -f $Item.KeyName) -Name "Start" -PropertyType "DWORD" -Value 0 -Force -ErrorAction Stop | Out-Null
}
Catch
{
Write-EventLog -EventId 150 -Message "Failed to add $($Item.KeyName)`n`n $($Error[0].Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Error
}
}
}
Else
{
Write-EventLog -EventId 50 -Message "No Autologgers found to disable" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Warning
Write-Verbose "No Autologgers found to disable"
}
}
Else
{
Write-EventLog -EventId 150 -Message "File not found: $AutoLoggersFilePath" -LogName 'Virtual Desktop Optimization' -Source 'AutoLoggers' -EntryType Error
Write-Warning "File Not Found: $AutoLoggersFilePath"
}
}
#endregion
#region Disable Services
If ($Optimizations -contains "Services" -or $Optimizations -contains "All")
{
$ServicesFilePath = ".\ConfigurationFiles\Services.json"
If (Test-Path $ServicesFilePath)
{
Write-EventLog -EventId 60 -Message "Disable Services" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Information
Write-Host "[VDI Optimize] Disable Services" -ForegroundColor Cyan
$ServicesToDisable = (Get-Content $ServicesFilePath | ConvertFrom-Json ).Where( { $_.VDIState -eq 'Disabled' })
If ($ServicesToDisable.count -gt 0)
{
Write-EventLog -EventId 60 -Message "Processing Services Configuration File" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Information
Write-Verbose "Processing Services Configuration File"
Foreach ($Item in $ServicesToDisable)
{
#Write-EventLog -EventId 60 -Message "Attempting to Stop Service $($Item.Name) - $($Item.Description)" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Information
#Write-Verbose "Attempting to Stop Service $($Item.Name) - $($Item.Description)"
#try
#{
# Stop-Service $Item.Name -Force -ErrorAction SilentlyContinue
#}
#catch
#{
# Write-EventLog -EventId 160 -Message "Failed to disable Service: $($Item.Name) `n $($_.Exception.Message)" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Error
# Write-Warning "Failed to disable Service: $($Item.Name) `n $($_.Exception.Message)"
#}
Write-EventLog -EventId 60 -Message "Attempting to disable Service $($Item.Name) - $($Item.Description)" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Information
Write-Verbose "Attempting to disable Service $($Item.Name) - $($Item.Description)"
Set-Service $Item.Name -StartupType Disabled
}
}
Else
{
Write-EventLog -EventId 60 -Message "No Services found to disable" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Warnnig
Write-Verbose "No Services found to disable"
}
}
Else
{
Write-EventLog -EventId 160 -Message "File not found: $ServicesFilePath" -LogName 'Virtual Desktop Optimization' -Source 'Services' -EntryType Error
Write-Warning "File not found: $ServicesFilePath"
} }
#endregion
#region Network Optimization
# LanManWorkstation optimizations
If ($Optimizations -contains "NetworkOptimizations" -or $Optimizations -contains "All")
{
$NetworkOptimizationsFilePath = ".\ConfigurationFiles\LanManWorkstation.json"
If (Test-Path $NetworkOptimizationsFilePath)
{
Write-EventLog -EventId 70 -Message "Configure LanManWorkstation Settings" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Host "[VDI Optimize] Configure LanManWorkstation Settings" -ForegroundColor Cyan
$LanManSettings = Get-Content $NetworkOptimizationsFilePath | ConvertFrom-Json
If ($LanManSettings.Count -gt 0)
{
Write-EventLog -EventId 70 -Message "Processing LanManWorkstation Settings ($($LanManSettings.Count) Hives)" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Verbose "Processing LanManWorkstation Settings ($($LanManSettings.Count) Hives)"
Foreach ($Hive in $LanManSettings)
{
If (Test-Path -Path $Hive.HivePath)
{
Write-EventLog -EventId 70 -Message "Found $($Hive.HivePath)" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Verbose "Found $($Hive.HivePath)"
$Keys = $Hive.Keys.Where{ $_.SetProperty -eq $true }
If ($Keys.Count -gt 0)
{
Write-EventLog -EventId 70 -Message "Create / Update LanManWorkstation Keys" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Verbose "Create / Update LanManWorkstation Keys"
Foreach ($Key in $Keys)
{
If (Get-ItemProperty -Path $Hive.HivePath -Name $Key.Name -ErrorAction SilentlyContinue)
{
Write-EventLog -EventId 70 -Message "Setting $($Hive.HivePath) -Name $($Key.Name) -Value $($Key.PropertyValue)" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Verbose "Setting $($Hive.HivePath) -Name $($Key.Name) -Value $($Key.PropertyValue)"
Set-ItemProperty -Path $Hive.HivePath -Name $Key.Name -Value $Key.PropertyValue -Force
}
Else
{
Write-EventLog -EventId 70 -Message "New $($Hive.HivePath) -Name $($Key.Name) -Value $($Key.PropertyValue)" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Host "New $($Hive.HivePath) -Name $($Key.Name) -Value $($Key.PropertyValue)"
New-ItemProperty -Path $Hive.HivePath -Name $Key.Name -PropertyType $Key.PropertyType -Value $Key.PropertyValue -Force | Out-Null
}
}
}
Else
{
Write-EventLog -EventId 70 -Message "No LanManWorkstation Keys to create / update" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Warning
Write-Warning "No LanManWorkstation Keys to create / update"
}
}
Else
{
Write-EventLog -EventId 70 -Message "Registry Path not found $($Hive.HivePath)" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Warning
Write-Warning "Registry Path not found $($Hive.HivePath)"
}
}
}
Else
{
Write-EventLog -EventId 70 -Message "No LanManWorkstation Settings foun" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Warning
Write-Warning "No LanManWorkstation Settings found"
}
}
Else
{
Write-EventLog -EventId 70 -Message "File not found - $NetworkOptimizationsFilePath" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Warning
Write-Warning "File not found - $NetworkOptimizationsFilePath"
}
# NIC Advanced Properties performance settings for network biased environments
Write-EventLog -EventId 70 -Message "Configuring Network Adapter Buffer Size" -LogName 'Virtual Desktop Optimization' -Source 'NetworkOptimizations' -EntryType Information
Write-Host "[VDI Optimize] Configuring Network Adapter Buffer Size" -ForegroundColor Cyan
Set-NetAdapterAdvancedProperty -DisplayName "Send Buffer Size" -DisplayValue 4MB -NoRestart
<# NOTE:
Note that the above setting is for a Microsoft Hyper-V VM. You can adjust these values in your environment...
by querying in PowerShell using Get-NetAdapterAdvancedProperty, and then adjusting values using the...
Set-NetAdapterAdvancedProperty command.
#>
}
#endregion
#region Local Group Policy Settings
# - This code does not:
# * set a lock screen image.
# * change the "Root Certificates Update" policy.
# * change the "Enable Windows NTP Client" setting.
# * set the "Select when Quality Updates are received" policy
If ($Optimizations -contains "LocalPolicy" -or $Optimizations -contains "All")
{
$LocalPolicyFilePath = ".\ConfigurationFiles\PolicyRegSettings.json"
If (Test-Path $LocalPolicyFilePath)
{
Write-EventLog -EventId 80 -Message "Local Policy Items" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Information
Write-Host "[VDI Optimize] Local Group Policy Items" -ForegroundColor Cyan
$PolicyRegSettings = Get-Content $LocalPolicyFilePath | ConvertFrom-Json
If ($PolicyRegSettings.Count -gt 0)
{
Write-EventLog -EventId 80 -Message "Processing PolicyRegSettings Settings ($($PolicyRegSettings.Count) Hives)" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Information
Write-Verbose "Processing PolicyRegSettings Settings ($($PolicyRegSettings.Count) Hives)"
Foreach ($Key in $PolicyRegSettings)
{
If ($Key.VDIState -eq 'Enabled')
{
If (Get-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -ErrorAction SilentlyContinue)
{
Write-EventLog -EventId 80 -Message "Found key, $($Key.RegItemPath) Name $($Key.RegItemValueName) Value $($Key.RegItemValue)" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Information
Write-Verbose "Found key, $($Key.RegItemPath) Name $($Key.RegItemValueName) Value $($Key.RegItemValue)"
Set-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -Value $Key.RegItemValue -Force
}
Else
{
If (Test-path $Key.RegItemPath)
{
Write-EventLog -EventId 80 -Message "Path found, creating new property -Path $($Key.RegItemPath) -Name $($Key.RegItemValueName) -PropertyType $($Key.RegItemValueType) -Value $($Key.RegItemValue)" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Information
Write-Verbose "Path found, creating new property -Path $($Key.RegItemPath) Name $($Key.RegItemValueName) PropertyType $($Key.RegItemValueType) Value $($Key.RegItemValue)"
New-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -PropertyType $Key.RegItemValueType -Value $Key.RegItemValue -Force | Out-Null
}
Else
{
Write-EventLog -EventId 80 -Message "Error: Creating Name $($Key.RegItemValueName), Value $($Key.RegItemValue) and Path $($Key.RegItemPath)" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Information
Write-Verbose "Error: Creating Name $($Key.RegItemValueName), Value $($Key.RegItemValue) and Path $($Key.RegItemPath)"
New-Item -Path $Key.RegItemPath -Force | New-ItemProperty -Name $Key.RegItemValueName -PropertyType $Key.RegItemValueType -Value $Key.RegItemValue -Force | Out-Null
}
}
}
}
}
Else
{
Write-EventLog -EventId 80 -Message "No LocalPolicy Settings Found!" -LogName 'Virtual Desktop Optimization' -Source 'LocalPolicy' -EntryType Warning
Write-Warning "No LocalPolicy Settings found"
}
}
}
#endregion
#region Edge Settings
If ($AdvancedOptimizations -contains "Edge" -or $AdvancedOptimizations -contains "All")
{
$EdgeFilePath = ".\ConfigurationFiles\EdgeSettings.json"
If (Test-Path $EdgeFilePath)
{
Write-EventLog -EventId 80 -Message "Edge Policy Settings" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Host "[VDI Advanced Optimize] Edge Policy Settings" -ForegroundColor Cyan
$EdgeSettings = Get-Content $EdgeFilePath | ConvertFrom-Json
If ($EdgeSettings.Count -gt 0)
{
Write-EventLog -EventId 80 -Message "Processing Edge Policy Settings ($($EdgeSettings.Count) Hives)" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Verbose "Processing Edge Policy Settings ($($EdgeSettings.Count) Hives)"
Foreach ($Key in $EdgeSettings)
{
If ($Key.VDIState -eq 'Enabled')
{
If ($key.RegItemValueName -eq 'DefaultAssociationsConfiguration')
{
Copy-Item .\ConfigurationFiles\DefaultAssociationsConfiguration.xml $key.RegItemValue -Force
}
If (Get-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -ErrorAction SilentlyContinue)
{
Write-EventLog -EventId 80 -Message "Found key, $($Key.RegItemPath) Name $($Key.RegItemValueName) Value $($Key.RegItemValue)" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Verbose "Found key, $($Key.RegItemPath) Name $($Key.RegItemValueName) Value $($Key.RegItemValue)"
Set-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -Value $Key.RegItemValue -Force
}
Else
{
If (Test-path $Key.RegItemPath)
{
Write-EventLog -EventId 80 -Message "Path found, creating new property -Path $($Key.RegItemPath) -Name $($Key.RegItemValueName) -PropertyType $($Key.RegItemValueType) -Value $($Key.RegItemValue)" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Verbose "Path found, creating new property -Path $($Key.RegItemPath) Name $($Key.RegItemValueName) PropertyType $($Key.RegItemValueType) Value $($Key.RegItemValue)"
New-ItemProperty -Path $Key.RegItemPath -Name $Key.RegItemValueName -PropertyType $Key.RegItemValueType -Value $Key.RegItemValue -Force | Out-Null
}
Else
{
Write-EventLog -EventId 80 -Message "Creating Key and Path" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Verbose "Creating Key and Path"
New-Item -Path $Key.RegItemPath -Force | New-ItemProperty -Name $Key.RegItemValueName -PropertyType $Key.RegItemValueType -Value $Key.RegItemValue -Force | Out-Null
}
}
}
}
}
Else
{
Write-EventLog -EventId 80 -Message "No Edge Policy Settings Found!" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Warning
Write-Warning "No Edge Policy Settings found"
}
}
Else
{
# nothing to do here"
}
}
#endregion
#region Remove Legacy Internet Explorer
If ($AdvancedOptimizations -contains "RemoveLegacyIE" -or $AdvancedOptimizations -contains "All")
{
Write-EventLog -EventId 80 -Message "Remove Legacy Internet Explorer" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Host "[VDI Advanced Optimize] Remove Legacy Internet Explorer" -ForegroundColor Cyan
Get-WindowsCapability -Online | Where-Object Name -Like "*Browser.Internet*" | Remove-WindowsCapability -Online
}
#endregion
#region Remove OneDrive Commercial
If ($AdvancedOptimizations -contains "RemoveOneDrive" -or $AdvancedOptimizations -contains "All")
{
Write-EventLog -EventId 80 -Message "Remove OneDrive Commercial" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Write-Host "[VDI Advanced Optimize] Removing OneDrive Commercial" -ForegroundColor Cyan
$OneDrivePath = @('C:\Windows\System32\OneDriveSetup.exe', 'C:\Windows\SysWOW64\OneDriveSetup.exe')
$OneDrivePath | ForEach-Object {
If (Test-Path $_)
{
Write-Host "`tAttempting to uninstall $_"
Write-EventLog -EventId 80 -Message "Commercial $_" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Start-Process $_ -ArgumentList "/uninstall" -Wait
}
}
Write-EventLog -EventId 80 -Message "Removing shortcut links for OneDrive" -LogName 'Virtual Desktop Optimization' -Source 'AdvancedOptimizations' -EntryType Information
Get-ChildItem 'C:\*' -Recurse -Force -EA SilentlyContinue -Include 'OneDrive','OneDrive.*' | Remove-Item -Force -Recurse -EA SilentlyContinue
}
#endregion
#region Disk Cleanup
# Delete not in-use files in locations C:\Windows\Temp and %temp%
# Also sweep and delete *.tmp, *.etl, *.evtx, *.log, *.dmp, thumbcache*.db (not in use==not needed)
# 5/18/20: Removing Disk Cleanup and moving some of those tasks to the following manual cleanup
If ($Optimizations -contains "DiskCleanup" -or $Optimizations -contains "All")
{
Write-EventLog -EventId 90 -Message "Removing .tmp, .etl, .evtx, thumbcache*.db, *.log files not in use" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Removing .tmp, .etl, .evtx, thumbcache*.db, *.log files not in use"
Get-ChildItem -Path c:\ -Include *.tmp, *.dmp, *.etl, *.evtx, thumbcache*.db, *.log -File -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -ErrorAction SilentlyContinue
# Delete "RetailDemo" content (if it exits)
Write-EventLog -EventId 90 -Message "Removing Retail Demo content (if it exists)" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Removing Retail Demo content (if it exists)"
Get-ChildItem -Path $env:ProgramData\Microsoft\Windows\RetailDemo\* -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -ErrorAction SilentlyContinue
# Delete not in-use anything in the C:\Windows\Temp folder
Write-EventLog -EventId 90 -Message "Removing all files not in use in $env:windir\TEMP" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Removing all files not in use in $env:windir\TEMP"
Remove-Item -Path $env:windir\Temp\* -Recurse -Force -ErrorAction SilentlyContinue -Exclude packer*.ps1
# Clear out Windows Error Reporting (WER) report archive folders
Write-EventLog -EventId 90 -Message "Cleaning up WER report archive" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Cleaning up WER report archive"
Remove-Item -Path $env:ProgramData\Microsoft\Windows\WER\Temp\* -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $env:ProgramData\Microsoft\Windows\WER\ReportArchive\* -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $env:ProgramData\Microsoft\Windows\WER\ReportQueue\* -Recurse -Force -ErrorAction SilentlyContinue
# Delete not in-use anything in your %temp% folder
Write-EventLog -EventId 90 -Message "Removing files not in use in $env:temp directory" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Removing files not in use in $env:temp directory"
Remove-Item -Path $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinue -Exclude packer*.ps1
# Clear out ALL visible Recycle Bins
Write-EventLog -EventId 90 -Message "Clearing out ALL Recycle Bins" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Clearing out ALL Recycle Bins"
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
# Clear out BranchCache cache
Write-EventLog -EventId 90 -Message "Clearing BranchCache cache" -LogName 'Virtual Desktop Optimization' -Source 'DiskCleanup' -EntryType Information
Write-Host "Clearing BranchCache cache"
Clear-BCCache -Force -ErrorAction SilentlyContinue
} #endregion
Set-Location $CurrentLocation
$EndTime = Get-Date
$ScriptRunTime = New-TimeSpan -Start $StartTime -End $EndTime
Write-EventLog -LogName 'Virtual Desktop Optimization' -Source 'VDOT' -EntryType Information -EventId 1 -Message "VDOT Total Run Time: $($ScriptRunTime.Hours) Hours $($ScriptRunTime.Minutes) Minutes $($ScriptRunTime.Seconds) Seconds"
Write-Host "`n`nThank you from the Virtual Desktop Optimization Team" -ForegroundColor Cyan
If ($Restart)
{
Restart-Computer -Force
}
Else
{
Write-Warning "A reboot is required for all changes to take effect"
}
######################## END OF SCRIPT ########################
}