Author Posts

January 30, 2015 at 6:04 am

I am having issues getting a True/FAlse Boolean to read correctly. I probably have some syntax wrong some place. Can you look at my code and let me know on the True/False statements what I have wrong?

clear
$Host.UI.RawUI.WindowTitle = "Profile Deletion Tool v1"
Write-Host "Profile Deletion Tool"
Write-Host "============================"
$pc = Read-Host "Computer Name"
$users = Get-WmiObject -Query "Select * From win32_userprofile" -ComputerName $pc
Write-Host "Active Profiles"
		Write-Host "-------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	else {
		$user = $user -replace "\\", "\\"
		$profile = $user.Substring(11)
		$profile
	}
}

$save = Read-Host "Which Profile do you wish to save?"
Write-Host "Saving $save ..."

clear

Write-Host "Summary of Actions"
Write-Host "========================"
Write-Host ""
Write-Host "Saving Profiles"
Write-Host "------------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	elseif($user -eq $save) {
		""
	}
	else {
		$path = $user
		$path = $path -replace "\\", "\\"
		$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
		if($loaded.Loaded -eq $True) {
			$user = $user -replace "\\", "\\"
			$profile = $user.Substring(11)
			Write-Host "$profile is currently active."
		}
		else {
			""
		}
	}
}
if($save -ne $null) {
	Write-Host "$save had been excluded."
}
Write-Host ""
Write-Host ""
Write-Host "Deleting Profiles"
Write-Host "------------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	elseif($user -eq $save) {
	
	}
	else {
		$path = $user
		$path = $path -replace "\\", "\\"
		$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
		if($loaded.Loaded -eq $True) {
			$user = $user -replace "\\", "\\"
			$profile = $user.Substring(11)
			Write-Host "$profile"
		}
		else {
			""
		}
	}
}

Write-Host "This is a PERMANANT DELETION!!!!" -ForegroundColor Red
$action = Read-Host "Are You Sure? (type Y for Yes and N for No)"
if($action -eq "y") {
	Write-Host "Deleting Selected Profiles" -ForegroundColor Cyan
	foreach($user in $users.LocalPath) {
		if($user -like "*Windows*") {
		""
		}
		elseif($user -like $save) {
		
		}
		else {
			$path = $user
			$path = $path -replace "\\", "\\"
			$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
			if($loaded.Loaded -ne $true) {
				$user = $user -replace "\\", "\\"
				$dprofile = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
				$dprofile.Delete()
			}
			else {
				""
			}
		}
	}
}
else {
	Write-Host "Nothing Deleted.  Please ReRun Script!!!" -ForegroundColor Green
}

I do know about Delprof2 and I am currently deploying that as a temporary solution to delete profiles. I am trying to create a more user friendly to save from accidently deleting the wrong profile and/or deleting all the profiles. I got this to work once and changed something, I just don't know what that something was.

January 30, 2015 at 10:04 am

Ok here is what I have come up with so far. My company wanted to ad some AD functionality to this so I have scrubbed some of the paths.

clear

Import-Module ActiveDirectory

$Host.UI.RawUI.WindowTitle = "Profile Deletion Tool v1"

Write-Host "Profile Deletion Tool"
Write-Host "============================"
$pc = Read-Host "Computer Name"
$users = Get-WmiObject -Query "Select * From win32_userprofile" -ComputerName $pc
$adserver = "ADSERVER PATH"
Write-Host ""
Write-Host "Active Profiles"
		Write-Host "-------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	else {
		try {
			$err.Clear() 
		}
		catch {
			""
		}
		$user = $user -replace "\\", "\\"
		$profile = $user.Substring(11)
		try {
			$saduser = Get-ADUser -Identity $profile -Server $adserver -Properties * -ErrorVariable err -ErrorAction SilentlyContinue
			if($err.Count -eq 0) {
				$adusername = $saduser.DisplayName
				Write-Host "$adusername | $profile"
			}
		}
		catch {
			Write-Host "$profile | Local Account"
		}
	}
}

$save = Read-Host "Which Profile do you wish to save? "
if([string]::IsNullOrEmpty($save)) {
	Write-Host "Are you sure you want to delete ALL profiles?" -ForegroundColor Yellow
	$none = Read-Host "Type YES to delete ALL"
	if($none -eq "Yes") {
		$save = "None"
	}
	else {
		Write-Host "Exiting Script" -ForegroundColor Red
		Start-Sleep -Seconds 2
		Exit
	}
}
Write-Host "Saving $save ..."

clear

Write-Host "Summary of Actions"
Write-Host "========================"
Write-Host ""
Write-Host "Saving Profiles"
Write-Host "-----------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	elseif($user -eq $save) {
		""
	}
	else {
		$err.Clear()
		$path = $user
		$path = $path -replace "\\", "\\"
		$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
		if($True -eq $loaded.Loaded) {
			$user = $user -replace "\\", "\\"
			$profile = $user.Substring(11)
			try {
				$saduser = Get-ADUser -Identity $profile -Server $adserver -Properties * -ErrorVariable err -ErrorAction SilentlyContinue
				if($err.Count -eq 0) {
					$adusername = $saduser.DisplayName
					Write-Host "$adusername | $profile (Currently Loaded)" -ForegroundColor Yellow
				}
			}
			catch {
				Write-Host "$profile | Local Account (Currently Loaded)" -ForegroundColor Yellow
			}
		}
	}
}
if($save) {
	Write-Host "Excluded Profiles"
	Write-Host "-----------------------"
	Write-Host "$save has been excluded." -ForegroundColor Green
}
Write-Host ""
Write-Host ""
Write-Host "Deleting Profiles"
Write-Host "-----------------------"
foreach($user in $users.LocalPath) {
	if($user -like "*Windows*") {
		""
	}
	elseif($user -like "*$save*") {
	
	}
	else {
		$err.Clear()
		$path = $user
		$path = $path -replace "\\", "\\"
		$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
		if($false -eq $loaded.Loaded) {
			$user = $user -replace "\\", "\\"
			$profile = $user.Substring(11)
			try {
				$saduser = Get-ADUser -Identity $profile -Server $adserver -Properties * -ErrorVariable err -ErrorAction SilentlyContinue
				if($err.Count -eq 0) {
					$adusername = $saduser.DisplayName
					Write-Host "$adusername | $profile" -ForegroundColor Red
				}
			}
			catch {
				Write-Host "$profile | Local Account" -ForegroundColor Red
			}
		}
	}
}

Write-Host "This is a PERMANANT DELETION!!!!" -ForegroundColor Red
$action = Read-Host "Are You Sure? (type Y for Yes and N for No)"
clear
if($action -eq "y") {
	foreach($user in $users.LocalPath) {
		if($user -like "*Windows*") {
		""
		}
		elseif($user -like "*$save*") {
		
		}
		else {
			$path = $user
			$path = $path -replace "\\", "\\"
			$loaded = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
			if($false -eq $loaded.Loaded) {
				$user = $user -replace "\\", "\\"
				$dprofile = Get-WmiObject -Query "Select * From Win32_UserProfile Where LocalPath='$path'" -ComputerName $pc
				$dprofile.Delete()
			}
		}
	}
	Write-Host "Deleted Unused Profiles" -ForegroundColor Cyan
	Write-Host "You may now exit." -ForegroundColor DarkCyan
}
else {
	Write-Host "Nothing Deleted.  Please ReRun Script!!!" -ForegroundColor Green
}

January 30, 2015 at 10:29 am

I didn't know about the Win32_UserProfile WMI class; that'll come in handy.

That said, the question is very vague. "Look through the code for true/false and what's wrong"? What exactly is wrong? Do you get an error? Reading true/false? I can't make sense of it, and I'm not inclined to read a fairly long script in detail. My suggestion to you would be to narrow down your problem to a test case if you can... Good luck.