Programmatically create libraries folder under Roaming profile for new users, as first desktop login does

93 Views Asked by At

I'm creating some new windows users and assigning admin rights with

net UserName Password /add
net localgroup administrators UserName /add

After this point, the user is created, but it's not really loaded. As for example, the C:\Users\UserName folder hasn't been created yet.

After that, I run some processes impersonating the user by using

psexec -i -h -u UserName -p Password process.exe

After this, some additional things are loaded for the user, I can see it on C:\Users\UserName, and on the system registry, but it's still missing part of the roaming profile.

In particular, I'm looking at the folder C:\Users\TestUser\AppData\Roaming\Microsoft\Windows\libraries not being created (Note: this is true only for win10, in win7 it does get created). This folder seems to be created only when logging with the user through the desktop login.

How can I finish initializing the user so that the user is loaded the same way that logging through the desktop would do it (or at least so that C:\Users\TestUser\AppData\Roaming\Microsoft\Windows\libraries is created).

I've tried using LoadUserProfile but I haven't had any luck so far

            IntPtr hUser = IntPtr.Zero;
            try
            {
                bool result = LogonUser(
                    userName, null, password,
                    LOGON_TYPE.LOGON32_LOGON_NETWORK,
                    LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT,
                    out hUser);
                if (result == false)
                {
                    Exception ex = new System.ComponentModel.Win32Exception(
                        Marshal.GetLastWin32Error());

                    throw ex;
                }

                PROFILEINFO profileInfo = new PROFILEINFO();
                profileInfo.dwSize = Marshal.SizeOf(profileInfo);
                profileInfo.lpUserName = Environment.MachineName+ @"\" + userName;
                result = LoadUserProfile(
                    hUser, ref profileInfo);

                if (result == false)
                {
                    Exception ex = new System.ComponentModel.Win32Exception(
                        Marshal.GetLastWin32Error());

                    throw ex;
                }
            }
            finally
            {
                if (hUser != IntPtr.Zero)
                {
                    CloseHandle(hUser);
                }
            }

Any solution on C#, powershell, or batch scripting would do it.

0

There are 0 best solutions below