-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-localprofiles.ps1
More file actions
71 lines (41 loc) · 2.03 KB
/
Copy pathRemove-localprofiles.ps1
File metadata and controls
71 lines (41 loc) · 2.03 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
<#Remove local profiles in loop by select number
there was bug in this function but i can't remember what exactly #>
function Remove-LocalProfiles {
param (
[string]$Computer
)
$continue = $true
while ($continue) {
$profileList = @()
try {
$profileFolders = Get-WmiObject -ComputerName $Computer -Query "SELECT * FROM Win32_UserProfile" | Where-Object { $_.Special -eq $false }
if ($profileFolders.Count -eq 0) {
Write-Host "No local profiles found on $Computer"
return
}
Write-Host "Local profiles found on $Computer"
for ($i = 0; $i -lt $profileFolders.Count; $i++) {
$profile = $profileFolders[$i]
$lastLoginTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($profile.LastUseTime)
Write-Host "$i. User: $($profile.LocalPath.Split('\')[-1]), Profile Path: $($profile.LocalPath), Last Login Time: $($lastLoginTime.ToShortDateString()) $($lastLoginTime.ToShortTimeString())"
}
$selection = Read-Host "Enter the number of the profile to remove (Q to quit)"
if ($selection -eq "Q" -or $selection -eq "q") {
$continue = $false
} elseif ($selection -ge 0 -and $selection -lt $profileFolders.Count) {
$profileToRemove = $profileFolders[$selection]
Write-Host "Removing local profile for $($profileToRemove.LocalPath)..."
$result = $profileToRemove.Delete()
if ($result.ReturnValue -eq 0) {
Write-Host "Local profile for $($profileToRemove.LocalPath) has been removed."
} else {
Write-Host "Removed"
}
} else {
Write-Host "Invalid selection."
}
} catch {
Write-Host "Error: $($_.Exception.Message)"
}
}
}