Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- ~~~
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- # .Net methods for hiding/showing the console in the background
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- function Show-Console
- {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- # Hide = 0,
- # ShowNormal = 1,
- # ShowMinimized = 2,
- # ShowMaximized = 3,
- # Maximize = 3,
- # ShowNormalNoActivate = 4,
- # Show = 5,
- # Minimize = 6,
- # ShowMinNoActivate = 7,
- # ShowNoActivate = 8,
- # Restore = 9,
- # ShowDefault = 10,
- # ForceMinimized = 11
- [Console.Window]::ShowWindow($consolePtr, 4)
- }
- function Hide-Console
- {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- #0 hide
- [Console.Window]::ShowWindow($consolePtr, 0)
- }
- # Check if the Active Directory PowerShell module is available
- function Test-ADModuleAvailable {
- $availableModules = Get-Module -ListAvailable
- return $availableModules.Name -contains "ActiveDirectory"
- }
- # Main function to manage remote profiles
- function Manage-RemoteProfiles {
- Hide-Console
- $form = New-Object System.Windows.Forms.Form
- $form.Text = 'Remote Profile Management Tool'
- $form.Size = New-Object System.Drawing.Size(400, 160)
- $form.StartPosition = 'CenterScreen'
- # Label for computer name input
- $label = New-Object System.Windows.Forms.Label
- $label.Location = New-Object System.Drawing.Point(10, 10)
- $label.Size = New-Object System.Drawing.Size(360, 20)
- $label.Text = 'Enter the computer name or IP (localhost for this computer):'
- $form.Controls.Add($label)
- # Text box for computer name input
- $textbox = New-Object System.Windows.Forms.TextBox
- $textbox.Location = New-Object System.Drawing.Point(10, 40)
- $textbox.Size = New-Object System.Drawing.Size(370, 20)
- $form.Controls.Add($textbox)
- # Button to fetch and delete profiles
- $button = New-Object System.Windows.Forms.Button
- $button.Location = New-Object System.Drawing.Point(10, 70)
- $button.Size = New-Object System.Drawing.Size(370, 30)
- $button.Text = 'Fetch and Manage Profiles'
- $button.Add_Click({
- $form.Close()
- Show-ProfileSelectionForm -RemoteComputerName $textbox.Text
- })
- $form.Controls.Add($button)
- # Show the form
- $form.ShowDialog()
- }
- # Function to fetch user profiles from the specified computer, display them, and handle deletion
- function Show-ProfileSelectionForm {
- param (
- [string]$RemoteComputerName
- )
- # Test PS Remoting
- try {
- $result = Invoke-Command -ComputerName $RemoteComputerName -ScriptBlock { $true } -ErrorAction Stop
- }
- catch {
- $result = $false
- }
- if ($result) {
- Write-Output "PS Remoting is already enabled on $RemoteComputerName."
- } else {
- Write-Output "PS Remoting is not enabled on $RemoteComputerName. Trying to enable it using PSexec..."
- # Use PSexec to enable PS Remoting on the remote computer
- psexec.exe \\$RemoteComputerName powershell -Command "Enable-PSRemoting -Force -SkipNetworkProfileCheck"
- # Check if PS Remoting is now enabled
- try {
- $resultAfter = Invoke-Command -ComputerName $RemoteComputerName -ScriptBlock { $true } -ErrorAction Stop
- }
- catch {
- $resultAfter = $false
- }
- if ($resultAfter) {
- Write-Output "Successfully enabled PS Remoting on $RemoteComputerName."
- } else {
- Write-Output "Failed to enable PS Remoting on $RemoteComputerName."
- }
- }
- # Check if AD module is available for additional name resolution
- $ADModuleAvailable = Test-ADModuleAvailable
- # Create a CIM session to the specified computer
- $cimSession = New-CimSession -ComputerName $RemoteComputerName
- # Fetch profiles using the CIM session
- $profiles = Get-CimInstance -CimSession $cimSession -ClassName Win32_UserProfile |
- Where-Object { $_.LocalPath -notlike 'C:\Windows*' } |
- Select-Object LocalPath, LastUseTime, SID
- # If the AD module is available, enrich profiles with first and last names
- if ($ADModuleAvailable) {
- Import-Module ActiveDirectory
- $profiles = $profiles | ForEach-Object {
- $sid = $_.SID
- $adUser = Get-ADUser -Filter "ObjectSID -eq '$sid'" -Properties GivenName, Surname -ErrorAction SilentlyContinue
- if ($adUser) {
- $_ | Add-Member -MemberType NoteProperty -Name "FullName" -Value ($adUser.GivenName + " " + $adUser.Surname) -Force
- } else {
- $_ | Add-Member -MemberType NoteProperty -Name "FullName" -Value "N/A" -Force
- }
- $_
- }
- }
- # Display the profiles in a grid for selection
- $selectedProfiles = $profiles | Out-GridView -PassThru -Title "Select profiles to delete"
- # If any profiles were selected, delete them
- # If any profiles were selected, delete them
- if ($selectedProfiles) {
- foreach ($sid in $selectedProfiles.SID) {
- # Fetch the profile directly using its SID and the CIM session
- $profile = Get-CimInstance -ClassName Win32_UserProfile -Filter "SID = '$sid'" -CimSession $cimSession
- if ($profile) {
- # Directly remove the fetched profile using the CIM session
- $profile | Remove-CimInstance -CimSession $cimSession
- }
- }
- [System.Windows.Forms.MessageBox]::Show("Selected profiles have been deleted.", "Deletion Complete")
- }
- # Clean up the CIM session
- $cimSession | Remove-CimSession
- }
- # Start the remote profile management process
- Manage-RemoteProfiles
- ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement