Skip to content

Commit 4ce837a

Browse files
author
Robin Stolpe
committed
update
1 parent 132802d commit 4ce837a

File tree

4 files changed

+167
-8
lines changed

4 files changed

+167
-8
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ All of the Windows scripts are stored in the [Windows folder](https://github.com
1919
- [Find-NeededModules](https://github.com/rstolpe/PowerShell-Scripts/blob/main/Windows/Find-NeededModules.md)
2020
This function are making sure that the needed modules are installed, up to date and imported.
2121
- [Show-MonitorInformation](https://github.com/rstolpe/PowerShell-Scripts/blob/main/Windows/Show-MonitorInformation.md)
22-
Returns information about all the monitors that has been connected to the local or remote computer
22+
Returns information about all the monitors that has been connected to the local or remote computer
23+
- [Remove-BrowserSettings](https://github.com/rstolpe/PowerShell-Scripts/blob/main/Windows/Remove-BrowserSettings.md)
24+
This function let you delete Edge or Chrome settings for a specific user from both local and remote computer.

Windows/Get-UserProfiles.ps1

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
}
4545
}
4646
catch {
47-
Write-Error "Something went wrong when collecting the user profiles!"
48-
Write-Error "$($PSItem.Exception.Message)"
47+
Write-Host "Something went wrong when collecting the user profiles!" -ForegroundColor Red
48+
Write-Host "$($PSItem.Exception.Message)" -ForegroundColor Red
4949
break
5050
}
5151
}
@@ -113,8 +113,8 @@ Function Remove-UserProfile {
113113
Write-Host "The user profile $($Profile.LocalPath.split('\')[-1]) are now deleted!" -ForegroundColor Green
114114
}
115115
catch {
116-
Write-Error "Something went wrong when trying to delete the user profile $($Profile.LocalPath.split('\')[-1])"
117-
Write-Error "$($PSItem.Exception.Message)"
116+
Write-Host "Something went wrong when trying to delete the user profile $($Profile.LocalPath.split('\')[-1])" -ForegroundColor Red
117+
Write-Host "$($PSItem.Exception.Message)" -ForegroundColor Red
118118
continue
119119
}
120120
}
@@ -131,13 +131,13 @@ Function Remove-UserProfile {
131131
Write-Host "The user profile $($user) are now deleted!" -ForegroundColor Green
132132
}
133133
catch {
134-
Write-Error "Something went wrong when trying to delete the user profile $($user)"
135-
Write-Error "$($PSItem.Exception.Message)"
134+
Write-Host "Something went wrong when trying to delete the user profile $($user)" -ForegroundColor Red
135+
Write-Host "$($PSItem.Exception.Message)" -ForegroundColor Red
136136
continue
137137
}
138138
}
139139
else {
140-
Write-Warning "$($user) did not have any user profile on the computer!"
140+
Write-Warning "$($user) did not have any user profile on $($Computer)!"
141141
}
142142
}
143143
}

Windows/Remove-BrowserSettings.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Remove-BrowserSettings
2+
This function let you delete Edge or Chrome settings for a specific user from both local and remote computer.
3+
4+
### You can do the following with the function
5+
- List all user profiles on local and remote computer.
6+
- Delete Edge or Chrome settings for specific user on local or remote computer.
7+
- It saves the bookmarks to C:\Temp before it deletes the settings and then restore them after.
8+
9+
### Links
10+
- [Readme](https://github.com/rstolpe/PowerShell-Scripts/blob/main/Windows/Remove-BrowserSettings.md)
11+
- [Script file](https://github.com/rstolpe/PowerShell-Scripts/blob/main/Windows/Remove-BrowserSettings.ps1)
12+
- [Blog post](https://stolpe.io/delete-edge-or-chrome-settings-on-remote-or-local-computer/)
13+
- [Report bug, issue, improvement request or request new script](https://github.com/rstolpe/PowerShell-Scripts/issues/new/choose)
14+
- [Guide, activate WinRM with GPO](https://stolpe.io/enable-winrm-with-gpo/)

Windows/Remove-BrowserSettings.ps1

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
function Remove-BrowserSettings {
2+
<#
3+
.SYNOPSIS
4+
Delete all settings for Chrome or Edge for a specified user
5+
6+
.DESCRIPTION
7+
This function will delete all of the Chrome or Edge settings for a specified user, it will also save the bookmarks to C:\Temp before deleting and restoring them after.
8+
9+
.PARAMETER Computer
10+
If you want to delete the settings on a remote computer you need to specify the computer name here, not needed if you going to use it on a local computer.
11+
12+
.PARAMETER UserName
13+
Write the username of the user that you want to delete the Edge or Chrome settings for.
14+
15+
.PARAMETER Edge
16+
Use this switch if you want to delete the settings for Edge
17+
18+
.PARAMETER Chrome
19+
Use this switch if you want to delete the settings for Chrome
20+
21+
.PARAMETER ListUsers
22+
When using this switch it will only return all of the user profiles on the computer
23+
24+
.EXAMPLE
25+
Remove-BrowserSettings -ListUsers
26+
This will list all of the user profiles that are on the local computer
27+
28+
Remove-BrowserSettings -Computer "Win11" -ListUsers
29+
This will list all fo the user profiles that are on the remote computer named "Win11"
30+
31+
Remove-BrowserSettings -Computer "Win11" -UserName "Robin" -Edge
32+
This will delete Edge settings for the user Robin on remote computer "Win11", if you want to delete Chrome settings just
33+
replace -Edge with -Chrome
34+
35+
Remove-BrowserSettings -UserName "Robin" -Edge
36+
This will delete Edge settings for the user Robin on local computer, if you want to delete Chrome settings just
37+
replace -Edge with -Chrome
38+
39+
#>
40+
41+
[CmdletBinding()]
42+
Param(
43+
[string]$Computer,
44+
[string]$UserName,
45+
[switch]$Edge,
46+
[switch]$Chrome,
47+
[switch]$ListUsers
48+
)
49+
if ($ListUsers -eq $False) {
50+
if ([string]::IsNullOrEmpty($UserName)) {
51+
Write-Host "You must enter a username!" -ForegroundColor Red
52+
Break
53+
}
54+
if ($Edge -eq $False -and $Chrome -eq $False) {
55+
throw "You must either delete Chrome or Edge"
56+
}
57+
}
58+
59+
if ($Edge -eq $True -and $Chrome -eq $True) {
60+
throw "You can't delete both Edge and Chrome at the same time!"
61+
}
62+
63+
if ([string]::IsNullOrEmpty($Computer)) {
64+
[string]$Computer = "localhost"
65+
}
66+
if ($Edge -eq $True) {
67+
$Browser = "Microsoft\Edge"
68+
$BrowserProcess = "edge.exe"
69+
}
70+
if ($Chrome -eq $True) {
71+
$Browser = "Google\Chrome"
72+
$BrowserProcess = "chrome.exe"
73+
}
74+
75+
$GetAllUsers = (Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Select-Object LocalPath | foreach-object { $_.LocalPath.split('\')[-1] })
76+
77+
if ($ListUsers -eq $true) {
78+
Write-Host "The following user profiles exists on $($Computer):`n"
79+
$GetAllUsers
80+
}
81+
else {
82+
if ($UserName -in $GetAllUsers) {
83+
try {
84+
# Setting up CIMSession to kill all the chrome.exe process.
85+
$CimSession = New-CimSession -ComputerName $Computer
86+
Write-Host "Stopping all of the active $($BrowserProcess)..."
87+
$ChromeProcess = Get-CimInstance -CimSession $CimSession -Class Win32_Process -Property Name | where-object { $_.name -eq "$($BrowserProcess)" }
88+
if ($Null -ne $ChromeProcess) {
89+
[void]($ChromeProcess | Invoke-CimMethod -MethodName Terminate)
90+
}
91+
Remove-CimSession -InstanceId $CimSession.InstanceId
92+
Write-Host "All $($BrowserProcess) are now stopped" -ForegroundColor Green
93+
}
94+
catch {
95+
Write-Host "Something went wrong when trying to stopp $($BrowserProcess)" -ForegroundColor Red
96+
Write-Host "$($PSItem.Exception.Message)" -ForegroundColor Red
97+
Break
98+
}
99+
try {
100+
Write-Host "Starting to delete all browser settings..."
101+
102+
# Deleting Chrome/Edge folder in the user profile but before that it copy the bookmarks to C:\Temp and then back to the correct folder so the bookmarks don't get lost.
103+
Invoke-Command -ComputerName $Computer -Scriptblock {
104+
Param(
105+
$UserName,
106+
$Browser
107+
)
108+
$BrowserPath = "$env:SystemDrive\Users\$($UserName)\AppData\Local\$($Browser)\User Data\"
109+
$BookmarkPath = "$env:SystemDrive\Users\$($UserName)\AppData\Local\$($Browser)\User Data\Default\Bookmarks"
110+
$BookmarkFolderPath = "$env:SystemDrive\Users\$($UserName)\AppData\Local\$($Browser)\User Data\Default\"
111+
112+
if (Test-Path -Path $BookmarkPath -PathType Leaf) {
113+
if (Test-Path -Path "$env:SystemDrive\Temp") {
114+
Copy-Item $BookmarkPath -Destination "$env:SystemDrive\Temp"
115+
}
116+
else {
117+
New-Item -Path "$env:SystemDrive\" -Name "Temp" -ItemType "directory" > $Null
118+
Copy-Item $BookmarkPath -Destination "$env:SystemDrive\Temp"
119+
}
120+
}
121+
122+
if (Test-Path -Path $BrowserPath) {
123+
Remove-Item $BrowserPath -Recurse -Force
124+
}
125+
if (Test-Path -Path "$env:SystemDrive\Temp\Bookmarks"-PathType Leaf) {
126+
New-Item -ItemType Directory -Force -Path $BookmarkFolderPath
127+
Copy-Item "$env:SystemDrive\Temp\Bookmarks" -Destination $BookmarkFolderPath
128+
Remove-Item "$env:SystemDrive\Temp\Bookmarks" -Recurse -Force
129+
}
130+
} -ArgumentList $UserName, $Browser
131+
}
132+
catch {
133+
Write-Host "Something went wrong when trying to delete the browser settings" -ForegroundColor Red
134+
Write-Host "$($PSItem.Exception.Message)" -ForegroundColor Red
135+
Break
136+
}
137+
}
138+
else {
139+
Write-Warning "$($UserName) don't have a user profile on $($Computer), see list below for all the user profiles that exists:`n"
140+
$GetAllUsers
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)