Advertisement
Guest User

Windows user profile remote deletion script

a guest
Mar 4th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 6.71 KB | Software | 0 0
  1. Found a ui based windows profile, deletion tool online. With a little AI assistance and manual, coding and debugging. I was able to make it work on remote machines, two close the power shell command line window, so it's only a UI and to make it so that it checks to see if there is a active directory module, and all that installed on the machine speed run from if so it will also list the name of the users for each profile if they are in an active directory. It uses the grid UI so you can select multiple profiles and delete them all at once and  filter, sort and search through the list with different criteria. I want to add more columns to it at some point but for now this service purpose pretty damn well.
  2.  
  3. ~~~
  4.  
  5. Add-Type -AssemblyName System.Windows.Forms
  6. Add-Type -AssemblyName System.Drawing
  7.  
  8.  
  9. # .Net methods for hiding/showing the console in the background
  10. Add-Type -Name Window -Namespace Console -MemberDefinition '
  11. [DllImport("Kernel32.dll")]
  12. public static extern IntPtr GetConsoleWindow();
  13.  
  14. [DllImport("user32.dll")]
  15. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  16. '
  17.  
  18. function Show-Console
  19. {
  20.    $consolePtr = [Console.Window]::GetConsoleWindow()
  21.  
  22.    # Hide = 0,
  23.    # ShowNormal = 1,
  24.    # ShowMinimized = 2,
  25.    # ShowMaximized = 3,
  26.    # Maximize = 3,
  27.    # ShowNormalNoActivate = 4,
  28.    # Show = 5,
  29.    # Minimize = 6,
  30.    # ShowMinNoActivate = 7,
  31.    # ShowNoActivate = 8,
  32.    # Restore = 9,
  33.    # ShowDefault = 10,
  34.    # ForceMinimized = 11
  35.  
  36.    [Console.Window]::ShowWindow($consolePtr, 4)
  37. }
  38.  
  39. function Hide-Console
  40. {
  41.    $consolePtr = [Console.Window]::GetConsoleWindow()
  42.    #0 hide
  43.    [Console.Window]::ShowWindow($consolePtr, 0)
  44. }
  45.  
  46.  
  47.  
  48. # Check if the Active Directory PowerShell module is available
  49. function Test-ADModuleAvailable {
  50.   $availableModules = Get-Module -ListAvailable
  51.   return $availableModules.Name -contains "ActiveDirectory"
  52. }
  53. # Main function to manage remote profiles
  54. function Manage-RemoteProfiles {
  55. Hide-Console
  56.   $form = New-Object System.Windows.Forms.Form
  57.   $form.Text = 'Remote Profile Management Tool'
  58.   $form.Size = New-Object System.Drawing.Size(400, 160)
  59.   $form.StartPosition = 'CenterScreen'
  60.   # Label for computer name input
  61.   $label = New-Object System.Windows.Forms.Label
  62.   $label.Location = New-Object System.Drawing.Point(10, 10)
  63.   $label.Size = New-Object System.Drawing.Size(360, 20)
  64.   $label.Text = 'Enter the computer name or IP (localhost for this computer):'
  65.   $form.Controls.Add($label)
  66.   # Text box for computer name input
  67.   $textbox = New-Object System.Windows.Forms.TextBox
  68.   $textbox.Location = New-Object System.Drawing.Point(10, 40)
  69.   $textbox.Size = New-Object System.Drawing.Size(370, 20)
  70.   $form.Controls.Add($textbox)
  71.   # Button to fetch and delete profiles
  72.   $button = New-Object System.Windows.Forms.Button
  73.   $button.Location = New-Object System.Drawing.Point(10, 70)
  74.   $button.Size = New-Object System.Drawing.Size(370, 30)
  75.   $button.Text = 'Fetch and Manage Profiles'
  76.   $button.Add_Click({
  77.       $form.Close()
  78.       Show-ProfileSelectionForm -RemoteComputerName $textbox.Text
  79.   })
  80.   $form.Controls.Add($button)
  81.   # Show the form
  82.   $form.ShowDialog()
  83. }
  84. # Function to fetch user profiles from the specified computer, display them, and handle deletion
  85. function Show-ProfileSelectionForm {
  86.   param (
  87.       [string]$RemoteComputerName
  88.   )
  89.  
  90.        # Test PS Remoting
  91.            try {
  92.               $result = Invoke-Command -ComputerName $RemoteComputerName -ScriptBlock { $true } -ErrorAction Stop
  93.            }
  94.            catch {
  95.               $result = $false
  96.            }
  97.  
  98.            if ($result) {
  99.               Write-Output "PS Remoting is already enabled on $RemoteComputerName."
  100.            } else {
  101.               Write-Output "PS Remoting is not enabled on $RemoteComputerName. Trying to enable it using PSexec..."
  102.  
  103.               # Use PSexec to enable PS Remoting on the remote computer
  104.               psexec.exe \\$RemoteComputerName powershell -Command "Enable-PSRemoting -Force -SkipNetworkProfileCheck"
  105.  
  106.               # Check if PS Remoting is now enabled
  107.               try {
  108.                   $resultAfter = Invoke-Command -ComputerName $RemoteComputerName -ScriptBlock { $true } -ErrorAction Stop
  109.               }
  110.               catch {
  111.                   $resultAfter = $false
  112.               }
  113.  
  114.               if ($resultAfter) {
  115.                   Write-Output "Successfully enabled PS Remoting on $RemoteComputerName."
  116.               } else {
  117.                   Write-Output "Failed to enable PS Remoting on $RemoteComputerName."
  118.               }
  119.            }
  120.  
  121.   # Check if AD module is available for additional name resolution
  122.   $ADModuleAvailable = Test-ADModuleAvailable
  123.   # Create a CIM session to the specified computer
  124.   $cimSession = New-CimSession -ComputerName $RemoteComputerName
  125.   # Fetch profiles using the CIM session
  126.   $profiles = Get-CimInstance -CimSession $cimSession -ClassName Win32_UserProfile |
  127.               Where-Object { $_.LocalPath -notlike 'C:\Windows*' } |
  128.               Select-Object LocalPath, LastUseTime, SID
  129.   # If the AD module is available, enrich profiles with first and last names
  130.   if ($ADModuleAvailable) {
  131.       Import-Module ActiveDirectory
  132.       $profiles = $profiles | ForEach-Object {
  133.           $sid = $_.SID
  134.           $adUser = Get-ADUser -Filter "ObjectSID -eq '$sid'" -Properties GivenName, Surname -ErrorAction SilentlyContinue
  135.           if ($adUser) {
  136.               $_ | Add-Member -MemberType NoteProperty -Name "FullName" -Value ($adUser.GivenName + " " + $adUser.Surname) -Force
  137.           } else {
  138.               $_ | Add-Member -MemberType NoteProperty -Name "FullName" -Value "N/A" -Force
  139.           }
  140.           $_
  141.       }
  142.   }
  143.   # Display the profiles in a grid for selection
  144.   $selectedProfiles = $profiles | Out-GridView -PassThru -Title "Select profiles to delete"
  145.   # If any profiles were selected, delete them
  146.   # If any profiles were selected, delete them
  147.    if ($selectedProfiles) {
  148.       foreach ($sid in $selectedProfiles.SID) {
  149.           # Fetch the profile directly using its SID and the CIM session
  150.           $profile = Get-CimInstance -ClassName Win32_UserProfile -Filter "SID = '$sid'" -CimSession $cimSession
  151.           if ($profile) {
  152.               # Directly remove the fetched profile using the CIM session
  153.               $profile | Remove-CimInstance -CimSession $cimSession
  154.           }
  155.       }
  156.       [System.Windows.Forms.MessageBox]::Show("Selected profiles have been deleted.", "Deletion Complete")
  157.    }
  158.   # Clean up the CIM session
  159.   $cimSession | Remove-CimSession
  160. }
  161. # Start the remote profile management process
  162. Manage-RemoteProfiles
  163.  
  164. ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement