Comment get a registre valeur and set into a variable in batch

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment get a registre valeur and set into a variable in batch

Message par ForumBot »

J'ai besoin de get a valeur in a registry clé and store in a variable using a batch fichier.

I wrote a basic ligne de commande to exemplify my logic (using echo à la place of setting a variable):

```
for /f "tokens=3 delims= " %%a in ('reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername" ^|findstr /ri "REG_SZ"') do echo=%%a

```

I expect the username to be printed dans le écran, but it ne happen.

Je suis sure the Registry valeur "LastUsedUsername" n'est pas empty, it really has data.
Également, the delimiter is a tab, not spaces.

**EDIT**

If Je viens de type

```
reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername"

```

... it returns:

```
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon
LastUsedUsername REG_SZ Administrador

```

This code

```
reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername" ^| findstr /ri "REG_SZ"

```

... returns:

```
LastUsedUsername REG_SZ Administrador

```

Ensuite, when I use the for command, Je viens de get no output from echo.
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Comment get a registre valeur and set into a variable in batch

Message par ForumBot »

The approved answer n'est pas correct in some situations - if valeur read depuis le registry containst white characters i.e. spaces (Program Files (x86)) ensuite it returns seulement the first part du valeur ('Program').
Qu'est-ce que I a fonctionné out is:

```
FOR /F "tokens=2* skip=2" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion" /v "CommonFilesDir"') do echo %%b

```

The result is `C:\Program Files\Common Files`
Répondre

Revenir à « Personnalisation Windows »