How to take ownership of a terminal user folder so you can safely remove it

How to take ownership of a terminal user folder so you can safely remove it

Introduction

This was an annoying issue for me to remove the a terminal server user’s profile from our file servers.

Each time you need to remove an user’s folder you get the standard access denied, then you need to change the permissions, inheritance, etc… so to save my colleagues and myself the trouble of repeating these steps, I’ve created a simple script in which you only need to enter the username folder and presto, the permissions are now your to delete.

I could remove it through the script as well, but its safer to do it manually as it works as a double check. Perhaps future wise it will be extended to do this with additional verification.

How to

The script itself has some minor checks in place to make sure you enter the correct username as this will remove ALL permissions of the original user and assign them to yours!

The commandlet below takes the actual ownership and changes the inheritance:

takeown /f $path\$username /r /d y

icacls $path\$username /q /c /t /reset

To change the path, simple change the VAR $path to your TS profiles folder location.
e.g $path = D:\Profiles\

Script

cls

# Execute forced ownership
function TakeOwnerShip
{
# Ask username
$username = Read-Host "Enter the username + profile version (e.g. evtai.v6)"

# Check if username is not empty
While ( ($username -eq $NULL) -or ($username -eq '') )
{
$username = Read-Host "Enter the username + profile version (e.g. evtai.v6)"
}

# Test if username (folder) is valid/ exists
$FileExists = Test-Path "D:\Profiles\$username"

# IF True execute take ownership rights
if($FileExists -eq $true)
{
Write-Host "Path is D:\Profiles\$username"
Write-Host ""

# Ask for confirmation before removal -> yes = continue, everyting else = cancel
$confirmation = Read-Host "Are you sure you wish to claim ownership of $username their profile? Type yes to continue"
if ($confirmation -eq 'yes')
{
takeown /f D:\Profiles\$username /r /d y

icacls D:\Profiles\$username /q /c /t /reset

Write-Host "Claimed ownership"
Write-Host ""

pause

Show-Menu
}

# Else cancel claiming and start again
else
{
Write-Host "Cancelled claiming"
Write-Host ""

TakeOwnerShip
}
}

# Start again if username does not exists
else
{
Write-Host "Username does not exists!"
Write-Host ""

TakeOwnerShip
}
}

# Create menu options
function Show-Menu
{
param
(
[string]$Title = 'Take ownership'
)

cls

Write-Host "================ $Title ================"
Write-Host ""
Write-Host "0: Take ownership of a TS Profile folder"
Write-Host ""
Write-Host "Q: Press 'Q' to quit"
Write-Host ""
}

# Loop over the menu options until user presses Q (or exits the program)
do
{
Show-Menu

$input = Read-Host "Please make a selection"
Write-Host ""

switch ($input)
{

'0' { TakeOwnerShip }

'q' { return }
}
pause
}
until ($input -eq 'q')

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *