-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-OldUserProfile.ps1
More file actions
135 lines (111 loc) · 6.45 KB
/
Copy pathRemove-OldUserProfile.ps1
File metadata and controls
135 lines (111 loc) · 6.45 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
<#
.SYNOPSIS
Findet verwaiste und alte Windows-Benutzerprofile und loescht sie.
.DESCRIPTION
Liest die ProfileList aus der Registry unter
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList, loest die
SIDs in Kontonamen auf und wertet die Lade- und Entladezeitstempel aus.
Profile, deren SID sich nicht mehr aufloesen laesst, erscheinen als
[UNKNOWN] - typischerweise bereits geloeschte Konten.
.NOTES
ACHTUNG: Remove-UserProfiles loescht Profilverzeichnisse endgueltig.
Vorher die Ausgabe von Get-ProfileListInfos pruefen und den Lauf zuerst
auf einem Testrechner durchspielen.
#>
function Get-ProfileListInfos() {
$profilelist = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$result = @()
foreach ($p in $profilelist) {
try {
$objUser = (New-Object System.Security.Principal.SecurityIdentifier($p.PSChildName)).Translate([System.Security.Principal.NTAccount]).value
}
catch {
$objUser = "[UNKNOWN]"
}
Remove-Variable -Force LTH, LTL, UTH, UTL -ErrorAction SilentlyContinue
$LTH = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileLoadTimeHigh -ErrorAction SilentlyContinue).LocalProfileLoadTimeHigh
$LTL = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileLoadTimeLow -ErrorAction SilentlyContinue).LocalProfileLoadTimeLow
$UTH = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileUnloadTimeHigh -ErrorAction SilentlyContinue).LocalProfileUnloadTimeHigh
$UTL = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileUnloadTimeLow -ErrorAction SilentlyContinue).LocalProfileUnloadTimeLow
$LoadTime = if ($LTH -and $LTL) {
[datetime]::FromFileTime("0x$LTH$LTL")
}
else {
$null
}
$UnloadTime = if ($UTH -and $UTL) {
[datetime]::FromFileTime("0x$UTH$UTL")
}
else {
$null
}
$result += [pscustomobject][ordered]@{
User = $objUser
SID = $p.PSChildName
Loadtime = $LoadTime
UnloadTime = $UnloadTime
}
}
return $result
}
function Resolve-SID {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[string]$sid
)
$objUser = (New-Object System.Security.Principal.SecurityIdentifier($sid)).Translate([System.Security.Principal.NTAccount]).value
return $objUser
}
function Remove-UserProfiles {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[string[]]$server,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
[PSCredential]$credentials
)
$allObj = @()
$test = $WhatIfPreference # Bei WhatIf ist es True, ansonsten False
$allObj += Invoke-Command -ComputerName $server -Credential $credentials -ScriptBlock {
$computer = $env:COMPUTERNAME
$testmodus = $using:test
# Löschen der ungültigen Profile
$myVALIDLocalPaths = Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Select-Object -Property @{Name = "FullName"; Expression = { $_.LocalPath } };
if ($myVALIDLocalPaths) {
Get-ChildItem -LiteralPath 'C:\Users' -Directory -Force | Where-Object Name -like "*BACKUP*" | Select-Object FullName | ForEach-Object {
if ($_.FullName -in $myVALIDLocalPaths.FullName) {
Write-Host($computer+ ": " + $_.FullName + " is Valid... do not delete")
} else {
Write-Host($computer + ": " + $_.FullName + "is NOT VALID. Trying to delete...") -ForegroundColor Red
Remove-Item $_.FullName -Force -Recurse -WhatIf:$testmodus
}
}
}
}
}
Get-ProfileListInfos
# Remove-UserProfiles -server $server -credentials $cred -WhatIf
# Holt einen Username anhand der SID
# Resolve-SID S-1-5-21-123456789-1234567890-1234567890-500
# Funktioniert nicht richtig
# Get-CimInstance -Class Win32_NetworkLoginProfile | Select-Object Name, FullName, @{Name = "LastLogon"; Expression = { [System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogon).ToString("dd MMMM yyyy HH:mm:ss") } } | Format-Table
Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Select-Object -Property LocalPath, Loaded, LastUseTime | Format-Table -AutoSize
# Zusammen mit der Funktion Resolve-SID
Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Select-Object -Property LocalPath, LastUseTime, @{Name = "ResolvedSID"; Expression = { Resolve-SID $_.SID } }
# Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Where-Object { $_.CreationTime -lt (get-date).adddays(-60) } | Where-Object { $_.Loaded -eq $false } | Select-Object -Property LocalPath, Loaded | Format-Table -AutoSize
# Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Where-Object { $_.CreationTime -lt (get-date).adddays(-60) } | Where-Object { $_.Loaded -eq $false } | Remove-CimInstance -Verbose #-Confirm:$false
Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special -and $_.LastUseTime -lt (get-date).adddays(-60) -and $_.Loaded -eq $false } | Select-Object -Property LocalPath, LastUseTime
# Get-CimInstance -ClassName Win32_UserProfile | Select-Object -Property LocalPath, LastUseTime , Special
Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special -and $_.LastUseTime -lt (get-date).adddays(-60) -and $_.Loaded -eq $false } | ForEach-Object { $_.LocalPath + ";" + $_.SID; Remove-CimInstance $_ -Verbose -WhatIf}
# # Löschen der ungültigen Profile (Nur Lokal)
# $myVALIDLocalPaths = Get-CimInstance -ClassName Win32_UserProfile | Where-Object { !$_.Special } | Select-Object -Property @{Name = "FullName"; Expression = { $_.LocalPath } };
# if ($myVALIDLocalPaths) {
# Get-ChildItem -LiteralPath 'C:\Users' -Directory -Force | Where-Object Name -like "*BACKUP*" | Select-Object FullName | ForEach-Object {
# if ($_.FullName -in $myVALIDLocalPaths.FullName) {
# Write-Host($_.FullName + " is Valid... do not delete")
# } else {
# Write-Host($_.FullName + "is NOT VALID. Try to delete...") -ForegroundColor Red
# Remove-Item $_.FullName -Force -Recurse
# }
# }
# }