-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveUserProfile.ps1
More file actions
267 lines (217 loc) · 11.4 KB
/
RemoveUserProfile.ps1
File metadata and controls
267 lines (217 loc) · 11.4 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
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
<#
.SYNOPSIS
Remediate script about user profile deletion for *student* accounts
.NOTES
Version: 0.1
Creation Date: 05-06-2023
Author: Ákos Bakos
Company: SmartCon GmbH
Contact: akos.bakos@smartcon.ch
Copyright (c) 2023 SmartCon GmbH
HISTORY:
Date By Comments
---------- --- ----------------------------------------------------------
05.06.2023 Akos Bakos Script created
20.11.2023 Akos Bakos Scheduled task extended with 'Clear-RecycleBin'
29.11.2023 Akos Bakos New 'Clear-RecycleBin' command + new versioning
30.11.2023 Akos Bakos Adding the shutdown tigger (event id 1)
#>
# scheduled task version
[int32]$Version = "4"
Function StartScript {
$scriptFolderPath = "$env:SystemDrive\Scripts"
$userConfigScriptPath = $(Join-Path -Path $scriptFolderPath -ChildPath "RemoveUserProfile_Script.ps1")
If(!(Test-Path -Path $scriptFolderPath)) {
New-Item -Path $scriptFolderPath -ItemType Directory -Force | Out-Null
}
Write-Host "Block inheritence for $scriptFolderPath"
$Acl = Get-Acl $scriptFolderPath
$Acl.SetAccessRuleProtection($true,$false)
$Acl | Set-Acl $scriptFolderPath
Write-Host "Getting Administrators group SID"
$SID_Admins = (New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")).Translate([System.Security.Principal.NTAccount])
Write-Host "Set ACL for Admins on $scriptFolderPath"
$Acl = Get-Acl $scriptFolderPath
$AccessRuleAdmins = New-Object System.Security.AccessControl.FileSystemAccessRule($SID_Admins, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$Acl.SetAccessRule($AccessRuleAdmins)
$Acl | Set-Acl $scriptFolderPath
Write-Host "Getting SYSTEM SID"
$SID_system = (New-Object System.Security.Principal.SecurityIdentifier("S-1-5-18")).Translate([System.Security.Principal.NTAccount])
Write-Host "Set ACL for SYSTEM on $scriptFolderPath"
$Acl = Get-Acl $scriptFolderPath
$AccessRuleSystem = New-Object System.Security.AccessControl.FileSystemAccessRule($SID_System, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$Acl.SetAccessRule($AccessRuleSystem)
$Acl | Set-Acl $scriptFolderPath
$userConfigScript = @"
<#
.SYNOPSIS
Delete user profiles on local or remote computer, the session in which you are running the script must be started with elevated user rights (Run as Administrator).
.DESCRIPTION
This script delete the user profiles on local o remote computer that match the search criteria.
.PARAMETER UserName
User Name to delete user profile, is possible use the '*' wildchar.
.PARAMETER ExcludeUserName
User name to exclude, is possible use the '*' wildchar.
.PARAMETER InactiveDays
Inactive days of the profile, this parameter is optional and specify that the profile will be deleted only if not used for the specifed days.
.PARAMETER ComputerName
Host name or list of host names on witch delete user profile, this parameter is optional (the default value is local computer).
.PARAMETER IncludeSpecialUsers
Include also special system service in the search, this parameter is optional (the default value is False).
.PARAMETER Force
Force execution without require confirm (the default value is False).
.EXAMPLE
./Remove-UserProfile.ps1 -UserName "LoganJ"
Delete the profile of the user with user name equal LoganJ.
.EXAMPLE
./Remove-UserProfile.ps1 -UserName "Logan*"
Delete all user profiles of the user with user name begin with "Logan".
.EXAMPLE
./Remove-UserProfile.ps1 -UserName "*" -InactiveDays 30
Delete all user profiles inactive by 30 days.
.EXAMPLE
./Remove-UserProfile.ps1 -UserName "*" -ExcludeUserName Admistrator
Delete all user profiles exclude user name Administrator
.EXAMPLE
./Remove-UserProfile.ps1 -UserName "*" -Force
Delete all user profiles without require confim
.KUDOS Ermanno Goletto - www.devadmin.it
.NOTES
Version: 0.1
Creation Date: 05-06-2023
Author: Akos Bakos
Company: SmartCon GmbH
Contact: akos.bakos@smartcon.ch
Copyright (c) 2023 SmartCon GmbH
HISTORY:
Date By Comments
---------- --- ----------------------------------------------------------
05.06.2023 Akos Bakos Script created
29.11.2023 Akos Bakos Clar-RecycleBin command
#>
[cmdletbinding(ConfirmImpact = 'High', SupportsShouldProcess=`$True)]
Param(
[Parameter(Mandatory=`$True)]
[string]`$UserName,
[string]`$ExcludeUserName = [string]::Empty,
[uint32]`$InactiveDays = [uint32]::MaxValue,
[string[]]`$ComputerName = `$env:computername,
[switch]`$IncludeSpecialUsers = `$False,
[switch]`$Force = `$True
)
`$Global:Transcript = "`$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-RemoveUserProfile.log"
Start-Transcript -Path (Join-Path "`$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\" `$Global:Transcript) -ErrorAction Ignore | Out-Null
Set-StrictMode -Version latest
ForEach (`$computer in `$ComputerName)
{
`$profileFounds = 0
Try {
`$profiles = Get-WmiObject -Class Win32_UserProfile -Computer `$computer -Filter "Special = '`$IncludeSpecialUsers'" -EnableAllPrivileges
} Catch {
Write-Warning "Failed to retreive user profiles on `$ComputerName"
Exit
}
ForEach (`$profile in `$profiles) {
`$sid = New-Object System.Security.Principal.SecurityIdentifier(`$profile.SID)
`$account = `$sid.Translate([System.Security.Principal.NTAccount])
`$accountDomain = `$account.value.split("\")[0]
`$accountName = `$account.value.split("\")[1]
`$profilePath = `$profile.LocalPath
`$loaded = `$profile.Loaded
`$lastUseTime = [System.Management.ManagementDateTimeConverter]::ToDateTime(`$profile.LastUseTime)
`$special = `$profile.Special
#Calculation of the login date
`$lastLoginDate = `$null
If (`$accountDomain.ToUpper() -eq `$computer.ToUpper()) {`$lastLoginDate = [datetime]([ADSI]"WinNT://`$computer/`$accountName").LastLogin[0]}
#Calculation of the unused days of the profile
`$profileUnusedDays=0
If (-Not `$loaded){
If(`$lastLoginDate -eq `$null){ `$profileUnusedDays = (New-TimeSpan -Start `$lastUseTime -End (Get-Date)).Days }
Else{`$profileUnusedDays = (New-TimeSpan -Start `$lastLoginDate -End (Get-Date)).Days}
}
If(`$accountName.ToLower() -Eq `$UserName.ToLower() -Or
(`$UserName.Contains("*") -And `$accountName.ToLower() -Like `$UserName.ToLower())) {
If(`$ExcludeUserName -ne [string]::Empty -And -Not `$ExcludeUserName.Contains("*") -And (`$accountName.ToLower() -eq `$ExcludeUserName.ToLower())){Continue}
If(`$ExcludeUserName -ne [string]::Empty -And `$ExcludeUserName.Contains("*") -And (`$accountName.ToLower() -Like `$ExcludeUserName.ToLower())){Continue}
If(`$InactiveDays -ne [uint32]::MaxValue -And `$profileUnusedDays -le `$InactiveDays){continue}
`$profileFounds ++
If (`$profileFounds -gt 1) {Write-Host "`n"}
Write-Host "Start deleting profile ""`$account"" on computer ""`$computer"" ..." -ForegroundColor Green
Write-Host "Account SID: `$sid"
Write-Host "Special system service user: `$special"
Write-Host "Profile Path: `$profilePath"
Write-Host "Loaded : `$loaded"
Write-Host "Last use time: `$lastUseTime"
If (`$lastLoginDate -ne `$null) { Write-Host "Last login: `$lastLoginDate" }
Write-Host "Profile unused days: `$profileUnusedDays"
If (`$loaded) {
Write-Warning "Cannot delete profile because is in use"
Continue
}
If (`$Force -Or `$PSCmdlet.ShouldProcess(`$account)) {
Try {
`$profile.Delete()
Write-Host "Profile deleted successfully" -ForegroundColor Green
} Catch {
Write-Host "Error during delete the profile" -ForegroundColor Red
}
}
}
}
If(`$profileFounds -eq 0){
Write-Warning "No profiles found on `$ComputerName with Name `$UserName"
}
}
Write-Host "Clear all recycle bins without confirmation"
`$recycleBinPath = Join-Path -Path `$env:SystemDrive -ChildPath '`$Recycle.Bin'
Remove-Item -Path `$recycleBinPath -Recurse -Force -ErrorAction SilentlyContinue
Stop-Transcript -Verbose | Out-File
"@
Out-File -FilePath $userConfigScriptPath -InputObject $userConfigScript -Encoding ascii
$taskName = "Remove User Profile Script v$Version"
#$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle Hidden -NonInteractive -Executionpolicy Bypass -File $userConfigScriptPath -UserName '*Student*'"
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument @"
-WindowStyle Hidden -NonInteractive -Executionpolicy Bypass -File $userConfigScriptPath -UserName "*Student*" -Force
"@
$trigger = New-ScheduledTaskTrigger -AtStartup
$class = Get-cimclass MSFT_TaskEventTrigger root/Microsoft/Windows/TaskScheduler
$trigger2 = $class | New-CimInstance -ClientOnly
$trigger2.Enabled = $True
$trigger2.Subscription = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name="Microsoft-Windows-Power-Troubleshooter"] and (EventID=1)]]</Select></Query></QueryList>'
$principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -StartWhenAvailable -Compatibility Win8
$task = New-ScheduledTask -Action $action -Trigger $trigger, $trigger2 -Principal $principal -Settings $settings
Register-ScheduledTask $taskName -InputObject $task -Force | Out-Null
$taskObject = Get-ScheduledTask -TaskName $taskName
$taskObject.Author = "Administrator"
$taskObject | Set-ScheduledTask | Out-Null
}
# Cleanup older scheduled tasks
try {
$Global:Transcript = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Create_RemoveUserProfile_ScheduledTask.log"
Start-Transcript -Path (Join-Path "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\" $Global:Transcript) -ErrorAction Ignore | Out-Null
Write-Host "Register a scheduled task to run for all users and execute the script on logon"
$schtaskName = "Remove User Profile Script v$Version"
$Target = Get-ScheduledTask | Where-Object { $_.TaskName -eq $schtaskName }
If (!$Target) {
Write-Host "Starting a scheduled task cleanup"
$Cleanups = Get-ScheduledTask | Where-Object { $_.TaskName -like "Remove User Profile Script v*" }
foreach ($Cleanup in $Cleanups) {
Write-Host "Cleaning: $Cleanup.Taskname"
Unregister-ScheduledTask $Cleanup.TaskName -Confirm:$false
}
Write-Host "Create 'Remove User Profile Script v$Version'"
StartScript
}
Else {
Write-Host "'Remove User Profile Script v$Version' scheduled task already exists"
}
Stop-Transcript
Exit 0
}
catch {
$errMsg = $_.Exception.Message
Write-Host $errMsg
Stop-Transcript
Exit 1
}