WorkInProgress/RemoteProfileRemoval.ps1
$inputXML = @"
<Window x:Class="Profile_Removal_Form.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Profile_Removal_Form" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" ResizeMode="NoResize" WindowStyle="None" Background="#181735" WindowStartupLocation="CenterScreen"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid Background="#0F0F2D" Grid.ColumnSpan="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="Users Being Removed" Grid.Column="2" Grid.ColumnSpan="5" Grid.RowSpan="3" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,30,0" FontFamily="Fonts/#Dosis" FontWeight="Medium" FontSize="20" Foreground="White" /> <TextBlock Text="Users To Keep" Margin="15,0,0,0" Foreground="White" Grid.Column="9" Grid.ColumnSpan="3" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Fonts/#Dosis" FontWeight="Medium" FontSize="20" /> </Grid> <ListBox x:Name="ListBoxLeft" Grid.Row="1" Grid.RowSpan="6" Margin="0,20,0,0" Width="300" BorderBrush="Black" Background="#FF040303" SelectionMode="Multiple" Foreground="#FF0BE2F1" FontSize="18" FontFamily="Font/#Dosis" /> <ListBox x:Name="ListBoxRight" Grid.Row="1" Grid.Column="1" Grid.RowSpan="6" Margin="0,20,0,0" Width="300" BorderBrush="Black" Background="#FF040303" Foreground="#FF0BE2F1" FontSize="18" FontFamily="Font/#Dosis"/> <Button x:Name="GetObjectsBtN" Content="Get Profiles" Foreground="White" Grid.Row="2" Background="#FE6584" Margin="360,0,360,20" Grid.ColumnSpan="2" FontWeight="Medium"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <Button x:Name="MoveLeftBtN" Content="<--" FontSize="18" Foreground="White" Grid.Row="4" Background="#FE6584" Margin="360,0,360,20" Grid.ColumnSpan="2" FontWeight="Medium"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <Button x:Name="MoveRightBtN" Content="-->" FontSize="18" Foreground="White" Grid.Row="3" Background="#FE6584" Margin="360,0,360,20" Grid.ColumnSpan="2" FontWeight="Medium"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <Button x:Name="ClearBtN" Content="Clear" Foreground="White" Grid.Row="5" Grid.ColumnSpan="2" Background="#FE6584" Margin="360,0,360,20" FontWeight="Medium"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <Button x:Name="ActionBtN" Content="Submit" Foreground="White" Grid.Row="8" Background="#FE6584" Margin="310,20,10,-22" FontWeight="Medium"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <Button x:Name="CancelBtN" Content="Cancel" Foreground="White" Grid.Row="8" Background="#FE6584" Margin="21,20,299,-22" FontWeight="Medium" Grid.Column="1" IsCancel="True"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5"/> </Style> </Button.Resources> </Button> <TextBlock x:Name="RemoteTxTB" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="68,33,0,0" Grid.Row="7" TextWrapping="Wrap" Text="ComputerName" FontSize="14" FontFamily="Font/#Dosis" FontWeight="Medium" Foreground="#FFDA29"/> <TextBox x:Name="RemoteComputerTxT" Margin="191,34,0,0" Grid.Row="7" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" FontFamily="Font/#Dosis" TextWrapping="Wrap" Text=""/> </Grid> </Window> "@ #Region "Xaml to Powershell code" Add-Type -AssemblyName PresentationFramework $InputXML = $InputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window' [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = $InputXML #Read XAML $Reader = (New-Object System.Xml.XmlNodeReader $Xaml) $Form = [Windows.Markup.XamlReader]::Load( $Reader ) $Xaml.SelectNodes("//*[@Name]") | % { Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) } #Add button, adds items from the left side listbox to the right. $WPFMoveRightBtN.Add_Click({ if ($WPFlistBoxLeft.selecteditem -ne $null) { while ($WPFlistBoxLeft.SelectedItems.Count -gt 0) { $WPFListBoxRight.Items.Add($WPFListBoxLeft.SelectedItem) $WPFlistBoxLeft.items.remove($WPFlistBoxLeft.SelectedItem) } } }) #Remove Button, takes items from the left side listbox and places them back in the right. $WPFMoveLeftBtN.Add_Click({ if ($WPFListBoxRight.selecteditem -ne $null) { while ($wpfListBoxRight.SelectedItems.Count -gt 0) { $WPFlistBoxLeft.Items.Add($wpfListBoxRight.SelectedItem) $wpfListBoxRight.items.remove($wpfListBoxRight.SelectedItem) } } }) #Clear out the form $WPFClearBtN.Add_Click({ $WPFlistBoxLeft.items.Clear() $WPFListBoxRight.items.Clear() }) #Retrieve profiles from remote computer $WPFGetObjectsBtN.Add_Click({ if ($WPFRemoteComputerTxT.text -gt 1 -eq $True) { $Profiles = invoke-command -ComputerName $WPFRemoteComputerTxT.Text -ScriptBlock { Get-UserProfile | Where-Object { ($_.LocalPath -match 'users') -and ($_.LocalPath -notmatch 'cov') } -Verbose } } $Profiles.localpath | ForEach-Object { $WPFListBoxLeft.Items.Add($_) } -Verbose }) $WPFActionBtN.Add_Click({ try { $RemoveUsers = Invoke-command -Computername $WPFRemoteComputerTxT.Text -Scriptblock { Remove-UserProfile -LocalPath $Using:WPFListBoxLeft.Items -Confirm:$False -ErrorAction Stop } } catch { return $_ } if($RemoveUsers -match 'The process cannot access the file because it is being used by another process.') { $MsgBoxInput = [System.Windows.MessageBox]::Show('The profile you are trying to remove is still currently logged in, please reboot the machine and try again.', 'User Profile Removal Script', 'Ok') } else { $WPFListBoxLeft.items.Clear() $WPFListBoxRight.items.Clear() $MsgBox = [System.Windows.MessageBox]::Show('User profiles have successfully been removed', 'User Profile Removal Script', 'Ok') $Form.Close() } }) $Form.ShowDialog() |