Alv45525 0 Light Poster

Hello guys,

I am working on something at work. We actually have a server whereby all the profiles of users working on citrix are stored. there is a routine work that we do to take ownership on a profile, rename the profile and copy the files of the old profile to the new one. WHen i say profile it is basically a folder with sub folders and files.

I wrote a little automation script on notepad ++ to do that but the thing is that there is no inheritance on the profiles so it is taking ownership only on the main folder and not on subfolders.

Kindly help

Find below what i wrote

:0001

set /p support=Enter your IT support username:%=%
set /p username=Enter The username:%=%

echo **************************************************
Echo * The Username set for IT Support is %support%.        *
Echo * The Username set for migration is %username%.  *
echo * It will be used throughout this session        *
echo **************************************************
pause

Echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo :: Note that you can stop the task any time by pressing ctrl+c.              ::
echo :: But then you will need to relaunch the batch file to perform other tasks  ::
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set dir="\\server01\profiles$"
::pushd \\server01\profiles$
echo off

::set /p username=Enter The username:%=%

if exist %dir%\%username% (

takeown /a /r /d y /f %dir%\%username%
icacls %dir%\%username% /grant %support%:F /t /q

)else (
echo ************************************
echo  Username %username% does not exist
echo ************************************
)

if exist %dir%\%username%.V2 (
takeown /a /r /d y /f %dir%\%username%.V2
icacls %dir%\%username%.V2 /grant %support%:F /t /q

)else (
echo *****************************************************
echo  There are no %username%V2 profile to take ownership
echo *****************************************************
)

pause

GOTO MENU

Dani AI

Generated

Good start — the symptoms you describe usually come down to three things: the way the UNC path is handled in the batch, where the commands are executed (local server vs remote share), and whether the ACLs/inheritance on subfolders are blocking propagation. In your posted script I noticed the dir variable is assigned with embedded quotes and pushd is commented out; that pattern commonly breaks recursive operations because the path expansion becomes malformed and some tools behave differently over a raw UNC.

Try these practical fixes (test on one profile first):

  • Fix the variable quoting and run against a drive mapped by pushd (avoids some UNC quirks).
  • Use icacls /setowner to set owner recursively, then grant the support account full control with recursion and continue-on-error switches.
  • If inheritance is explicitly disabled and you want inherited ACLs applied, re-enable inheritance first or use icacls /reset carefully (this removes custom ACEs — back up first).

Example (run elevated, on a test profile):

set "dir=\\server01\profiles$"
pushd "%dir%"
set "profilePath=%CD%\%username%"

rem set owner recursively (run elevated)
icacls "%profilePath%" /setowner "BUILTIN\Administrators" /T /C

rem grant support account full control recursively
icacls "%profilePath%" /grant:r "%support%:(F)" /T /C

popd

Troubleshooting tips: run the batch from the file server console (or map the share with pushd) to avoid remote permission limitations; verify owner with PowerShell: powershell -command "Get-Acl -Path '%profilePath%' | Select-Object -ExpandProperty Owner"; inspect ACLs with icacls "%profilePath%". If files still resist, look for explicit Deny ACEs or protected system files and try a targeted icacls /reset or re-enable inheritance with icacls /inheritance:e /T /C — but back up before doing destructive resets. Finally, when copying profiles, prefer robocopy with /COPYALL to preserve ACLs and attributes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.