-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveSetup
More file actions
65 lines (54 loc) · 2.5 KB
/
ActiveSetup
File metadata and controls
65 lines (54 loc) · 2.5 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
# Define a path on the target device you want to place your user script
$baseDestinationPath = "C:\Packages\Scripts"
# Define your script name (note this needs to be defined in the user script on line 14 also)
$ScriptName = "directorycleanup"
$ScriptExtension = ".ps1"
# To force a rerun of the script the versions can be incremented and this can be re-deployed to the target device
# which will force the scrip to rerun for the user
$ScriptMajorVersion = "1"
$ScriptMinorVersion = "0"
$ScriptBuildVersion = "0"
$PowershellPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
# Define the script content
# Note the script inside the $scriptContent variable will be created and run in the user context.
$scriptContent = @'
# Script Start #
$pathsToDelete = @(
"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache",
"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache"
)
foreach ($path in $pathsToDelete) {
if (Test-Path $path) {
try {
Remove-Item -Path $path -Recurse -Force -ErrorAction Stop
Write-Output "Deleted: $path"
} catch {
Write-Warning "Failed to delete $path - $_"
}
} else {
Write-Output "Path not found (skipped): $path"
}
}
# Script End #
'@
if (-Not (Test-Path $baseDestinationPath)) {
New-Item -Path $baseDestinationPath -ItemType Directory
Write-Output "Created destination directory at $baseDestinationPath"
}
# Define the file path Script
$filePath = "$baseDestinationPath\$ScriptName$ScriptExtension"
# Create the script file
Set-Content -Path $filePath -Value $scriptContent
Write-Host "$ScriptName.ps1 has been created at $filePath."
# Define the command to run the script with Bypass and non-blocking execution
$runCommand = "`"$PowershellPath`" -Command Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -WindowStyle Hidden -File `"$filePath`"'"
# Define the Active Setup registry key path
$ActiveSetupKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\$ScriptName"
# Create the Active Setup registry key
New-Item -Path $ActiveSetupKey -Force
$Version = "$ScriptMajorVersion,$ScriptMinorVersion,$ScriptBuildVersion"
# Set the values for the Active Setup component
Set-ItemProperty -Path $ActiveSetupKey -Name "(Default)" -Value "$ScriptName"
Set-ItemProperty -Path $ActiveSetupKey -Name "StubPath" -Value $runCommand
Set-ItemProperty -Path $ActiveSetupKey -Name "Version" -Value $Version
Write-Host "Active Setup key has been created for $ScriptName."