Clean-UserProfile.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID e8aaf715-036e-4ec9-b74e-67eb7093ce0f
 
.AUTHOR Shihan Pietersz
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Profile User Profile User
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>
 



<#
 
.DESCRIPTION
Clean up local user profiles
 
#>
 

param
(
[CmdletBinding()]
        [Parameter(Mandatory=$false,ParameterSetName ='ComputerName', ValueFromPipeline=$True, Position=0)]
        [string]$ComputerName
)

$ErrorActionPreference = "silentlycontinue"
Start-Transcript -Path .\ExecutionLog.txt 
try 
{
    
            if(!$ComputerName)
            {
            $Selection = "LocalHost"
            $ComputerName = 'Localhost'
            $exec = Read-Host -Prompt "Execute profile cleanup on local computer ? (Type YES to continue) :"
            }
            else
            {
            $Selection = "RemoteHost"
            $exec = Read-Host -Prompt "Execute profile cleanup on $($ComputerName) ? (Type YES to continue) :"
            }
    

    
                if($exec -eq "YES")
                    {
                    $UserProfiles = Get-WmiObject -ComputerName $ComputerName win32_UserProfile | Where-Object {$_.LocalPath -like 'C:\Users\*'}
                        foreach ($User in $UserProfiles)
                        {
                            Remove-WmiObject -InputObject $User
                            Write-Host "Profile $($User.LocalPath) has been removed" -ForegroundColor Green 
                        }
                        Write-Host "Script Ended"
                        Stop-Transcript
                        exit                 
                    }
                else
                    {
                    Write-Host "Script Ended"
                    Stop-Transcript 
                    exit
                    }
  

}

catch {
    $ErrorMessage = $_.Exception.Message
    Write-Host $ErrorMessage      
            }




# SIG # Begin signature block
# MIIIWAYJKoZIhvcNAQcCoIIISTCCCEUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEA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