Powershell/Support/user_profil_loeschen.ps1
2025-04-23 22:56:06 +00:00

49 lines
2.3 KiB
PowerShell

# Definieren Sie den Namen des Remote-Computers
$remoteComputer = Read-Host "Bitte geben Sie die Maschinennummer (Hostname oder IP) ein"
# Stellen Sie eine WMI-Verbindung zum Remote-Computer her
$credential = Get-Credential # Hier werden Sie aufgefordert, Benutzername und Passwort einzugeben
# Hauptloop, um Benutzerprofile zu löschen
do {
# Abfrage der aktuellen angemeldeten Benutzer
$currentlyLoggedInUser = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $remoteComputer -Credential $credential
# Ausgabe des aktuell angemeldeten Benutzers
Write-Output "Aktuell angemeldeter Benutzer: $($currentlyLoggedInUser.UserName)"
# Abfrage aller Benutzerprofile
$query = "SELECT * FROM Win32_UserProfile"
$userProfiles = Get-WmiObject -Class Win32_UserProfile -ComputerName $remoteComputer -Credential $credential
# Ausgabe der Profilinformationen und Sammlung der Profilpfade in ein Array
$profilePaths = @()
foreach ($profile in $userProfiles) {
Write-Output "Profil von Benutzer: $($profile.LocalPath)"
$profilePaths += $profile.LocalPath
}
# Der Benutzer wählt ein Profil, das gelöscht werden soll
$selectedProfile = $host.ui.PromptForChoice("Wählen Sie ein Profil zum Löschen", "Wählen Sie das Benutzerprofil, das Sie löschen möchten:", $profilePaths, 0)
# Korrektes Formatieren des Pfades für die WMI-Abfrage
$formattedPath = $profilePaths[$selectedProfile] -replace '\\', '\\'
# Bestätigen der Löschung
$confirmation = $host.ui.PromptForChoice("Bestätigung", "Sind Sie sicher, dass Sie das Profil `'$formattedPath'` löschen möchten?", @("Ja", "Nein"), 1)
if ($confirmation -eq 0) {
# Administrative Rechte sind erforderlich
$deleteProfile = Get-WmiObject -Class Win32_UserProfile -Filter "LocalPath = '$formattedPath'" -ComputerName $remoteComputer -Credential $credential
$deleteProfile.Delete()
Write-Output "Profil `'$formattedPath'` wurde gelöscht."
} else {
Write-Output "Löschung abgebrochen."
}
# Abfrage, ob der Benutzer ein weiteres Profil löschen möchte
$continue = $host.ui.PromptForChoice("Weitermachen", "Möchten Sie ein weiteres Benutzerprofil löschen?", @("Ja", "Nein"), 1)
} while ($continue -eq 0)
Write-Output "Programm beendet."