Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Last active January 5, 2018 16:53
Show Gist options
  • Save alexinnes/b34b6b7c1b8afb62a7f7642e5106e958 to your computer and use it in GitHub Desktop.
Save alexinnes/b34b6b7c1b8afb62a7f7642e5106e958 to your computer and use it in GitHub Desktop.
Able to remove profiles for computers on the network without having to have delprof
function Remove-Profile {
[CmdletBinding()]
param (
[Parameter(Mandatory=$false, Position=1)]
#Default Param is the local computer.
$Computer = $env:COMPUTERNAME,
[Parameter(Mandatory=$True, Position=0)]
[string]$Username
)
begin {
#Checks to see if the profile folder is on the computer.
IF(!(Test-Path -path "\\$Computer\c$\Users\$Username")){
Write-Error -Exception "Cannot find user profile, please confrm $Username is on $Computer" -ErrorAction Stop
}
$localProfilePath = "C:\\Users\\$Username"
}
process {
#Gets the profile
$WMIQuery = "SELECT * FROM Win32_UserProfile WHERE localpath = '$localProfilePath'"
$profile = Get-WmiObject -Query $WMIQuery -ComputerName $Computer
If($profile.loaded){
Write-Error "Cannot delete profile, profile is currently loaded." -ErrorAction Stop
}
Remove-WmiObject -InputObject $profile
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment