Last active
January 5, 2018 16:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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