'Invalid parameter "%Username%"' error when trying to grant permission by Icacls to current user

2.5k Views Asked by At

From this answer to question Windows SSH: Permissions for 'private-key' are too open I knew about command

Icacls "C:\Users\XX\Path\To\File" /Grant:r "%Username%":"(R)"

but in my environment (windows 10) it does not work:

Invalid parameter "%Username%"

Why it happen and how to fix it?

2

There are 2 best solutions below

3
Leandro Paixão On

According to the documentation it is necessary to inform the user for the permission:

Icacls "C:\Users\XX\Path\To\File" /Grant:r takeshi:"(R)"

But the example above will only work for commands executed via CMD. The message that is appearing means that the environment variable is empty. Why I can't tell you.

If you are using PowerShell you will have to change the $Username variable, in my example:

icacls.exe "C:\Temp\test.txt" /Grant:r "$Username":"(R)"

In Powershell I got the following response:

1 fichiers correctement traités ; échec du traitement de 0 fichiers
0
swani14 On

set var=whoami

Icacls "C:\Users\test" /Grant:r %var%:"(R)"