1
votes

The following script makes a backup of the data of each user's windows profile as long as each user has a folder in the backup location :

@echo off

set Target=D:\backup

for /f "tokens=*" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*"') do if exist "%Target%\%%~nXI\" (

........

)

pause 
exit

I need to change the logon name of one user. After changing it in the Active Directory, the user's profile folder (C:\Users) keeps the old name.

From what I understand : the SID is linked to AD account and changing username and other properties are without effect on user's windows profile.

I'm wondering if there is a way to get the new name in batch from its SID ? If yes, how ?

Any help would help me so much.

1
I'm not aware of any way to do it in winbatch, but I routinely do this in PowerShell. Would a PowerShell solution be acceptable? - Jeff Zeitlin
Type set and look at your options, eg userprofile. - user14797724
Something like wmic useraccount where (sid='S-1-<RestOfTheSID>' and domain='%userdomain%') get name I think - Theo

1 Answers

0
votes

If you want to get all usernames, you can use % as wilcard

CommandLine :

wmic useraccount get Name,SID

wmic useraccount where "sid like 'S-1-5-21%'" get name

And when you use it with a batch file, you should double it with %% to escape it !

Batch File

@echo off
wmic useraccount where "sid like 'S-1-5-21%%'" get name
pause

You can give a try with this batch file , just modify to your desired SID if you know it !

@echo off
Title Batch Getting user name from Active Directory using SID
Set "SID=S-1-5-21-1718693495-2001798431-3107361897-500"
Set "UserName="
for /f "skip=1 delims=" %%a in ('"wmic useraccount where sid="%%SID%%" get name"') do (
    for /f "delims=" %%b in ("%%a") do if not defined UserName set "UserName=%%~nb"
)
echo UserName = "%UserName%"
pause