-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-Shortcuts.ps1
131 lines (109 loc) · 4.97 KB
/
Install-Shortcuts.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
#Requires -Version 5.1
<#
.SYNOPSIS
Install context menu items in SHELL to quickly open items in a Windows Sandbox.
.DESCRIPTION
-None-
.PARAMETER InstallDirectory
An existing directory where this script can copy its runtime dependencies into.
.PARAMETER DarkModeIcon
Add a white icon to the context menu items that launch the containers.
.EXAMPLE
Install-Shortcuts
.EXAMPLE
Install-Shortcuts -InstallDirectory 'C:\Program Files\OpenInContainer' -DarkModeIcon
.LINK
https://github.com/Bert-Proesmans/WindowsSandboxShortcuts
.LINK
More information about Windows Sandbox can be found here;
https://learn.microsoft.com/en-us/windows/security/application-security/application-isolation/windows-sandbox/windows-sandbox-overview
.COMPONENT
This script requires Windows Optional Feature 'Containers-DisposableClientVM'
.NOTES
Author: Bert Proesmans
Install the required Windows Optional Feature 'Containers-DisposableClientVM' by
executing the following command in an elevated powershell prompt;
`Enable-WindowsOptionalFeature -Online -FeatureName Containers-DisposableClientVM`
#>
param (
[ValidateScript({
if (-Not ($_ | Test-Path) ) {
throw "Folder does not exist."
}
if (-Not ($_ | Test-Path -PathType Container) ) {
throw "The InstallDirectory argument must be a directory. File paths are not allowed."
}
return $true
})]
[string]$InstallDirectory = '.', # Current working directory
[switch]$DarkModeIcon
)
# WARN; Variable types are sticky, so we manually override the type once
[System.Management.Automation.PathInfo]$InstallDirectory = Resolve-Path $InstallDirectory
$SandboxIconPath = '%SystemRoot%\System32\WindowsSandbox.exe'
if ($DarkModeIcon.IsPresent) {
$SandboxIconPath = '%SystemRoot%\System32\WindowsSandbox.exe,1'
}
$BootstrapScriptPath = $InstallDirectory | Join-Path -ChildPath 'Start-Container.ps1'
$ParentPath = Resolve-Path $PSScriptRoot
if ($ParentPath.Path -ne $InstallDirectory.Path) {
$FilesToCopy = @(
$ParentPath | Join-Path -ChildPath 'Start-Container.ps1'
)
Copy-Item -Path $FilesToCopy -Destination $InstallDirectory -Force | Out-Null
}
# NOTE; The default powershell interpreter is called directly. If a different powershell is required;
# * 'Get-Command powershell).Definition' to use powershell on install user's PATH
# * 'powershell.exe' to use the powershell on runtime user's PATH
#$PowershellPath = (Get-Command powershell).Definition
$PowershellPath = "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
$CommandScript = @(
# ERROR; Command line is parsed by command processor so grouping words requires double quotes (")!
# Single quotes (') would be seen as a literal, not a grouping indicator.
"""$PowershellPath"""
, '-NoProfile'
, '-ExecutionPolicy ByPass'
, "-File ""$BootstrapScriptPath"""
, "-TargetPath ""%V"""
)
Function New-QuickItem {
param (
$FileCommonName,
$FileExtension,
$ItemLabel = "Run $FileExtension in a sandbox",
[string[]]$CustomPath = @()
)
$ClassesPath = (@("Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Classes", $FileCommonName) + $CustomPath) -join '\'
$QuickItemPath = $ClassesPath, 'Shell', $ItemLabel -join '\'
$QuickItemCommandPath = $QuickItemPath, 'Command' -join '\'
# New-Item -Path $QuickItemPath -Force
New-Item -Path $QuickItemCommandPath -Force | Out-Null
$Properties = @(
@{
Path = $QuickItemPath
Name = 'icon'
PropertyType = 'String'
Value = $script:SandboxIconPath
}
@{
Path = $QuickItemCommandPath
Name = '(Default)'
PropertyType = 'ExpandString'
Value = "$($script:CommandScript)"
}
)
$Properties | ForEach-Object -Process { New-ItemProperty @_ -Force } | Out-Null
}
# ProgID items, which depend on installed software state
# The filetypes are from windows core, so we can composite our config into the registry safely
New-QuickItem -FileCommonName 'Directory' -ItemLabel "Open folder in a sandbox"
New-QuickItem -FileCommonName 'Directory' -CustomPath "Background" -ItemLabel "Open folder in a sandbox"
New-QuickItem -FileCommonName 'exefile' -FileExtension 'EXE'
New-QuickItem -FileCommonName 'Msi.Package' -FileExtension 'MSI'
# Stable file associations independent of software state
# REF; https://learn.microsoft.com/en-us/windows/win32/shell/app-registration#registering-verbs-and-other-file-association-information
New-QuickItem -CustomPath 'SystemFileAssociations', '.pdf' -FileExtension 'PDF'
New-QuickItem -CustomPath 'SystemFileAssociations', '.ps1' -ItemLabel "Open script in a sandbox"
New-QuickItem -CustomPath 'SystemFileAssociations', '.cmd' -ItemLabel "Open script in a sandbox"
New-QuickItem -CustomPath 'SystemFileAssociations', '.bat' -ItemLabel "Open script in a sandbox"
# TODO; REG ..