setx command: WARNING: The data being saved is truncated to 1024 characters has removed a lot of my user env variables

simo2000 1 Reputation point
2022-06-23T11:49:46.793+00:00

hi,

it seems that setx command has truncated my PATH

H:\>setx PATH "%PATH%;C:\Apps\bin"

WARNING: The data being saved is truncated to 1024 characters.

SUCCESS: Specified value was saved.

In the last 3 years, each time I install a software it gets added to my normal env variable. so my env was quite long. and now I have run setx command and I lost a lot my path variables

is there an option to show a warning before truncating ?

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-06-27T07:49:46.643+00:00

    Hello,

    Thanks for posting the query here and we are happy to help you.

    If you wish to recover the path before its truncating, please verify the registry from your recovery point.

    User Variables
    HKEY_CURRENT_USER\Environment

    System Variables
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

    If you have system restore points, you can find the relevant key and restore it.

    https://support.microsoft.com/en-us/windows/recovery-options-in-windows-31ce2444-7de3-818c-d626-e3b5a3024da5

    Or you could use a PowerShell script similar to the following:
    $newPath = 'F:\common tools\git\bin;F:\common tools\python\app;F:\common tools\python\app\scripts;F:\common tools\ruby\bin;F:\masm32\bin;F:\Borland\BCC55\Bin'
    $oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
    [Environment]::SetEnvironmentVariable('PATH', "$newPath;$oldPath",'Machine');

    Refer this link for detailed steps.
    https://gist.github.com/drazul/b92f780689bd89a0d2a7

    There are no option to give warning while the truncation happens, but if you are using windows 8 or higher, you can make a symbolic link to the folder. for example:

    mklink /d C:\pf "C:\Program Files"
    mklink /d C:\pf86 "C:\Program Files (x86)"

    This would make a link so "c:\pf" would be your program files folder

    Here is a helpful page with information about Windows PowerShell

    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/powershell

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer.--

    Thank you!

    0 comments No comments

  2. simo2000 1 Reputation point
    2022-06-27T13:30:10.147+00:00

    hi

    unfortunately this PowerShell command is not working. And on my working pc I don't have permission to modify anything.

    PS H:\> $oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
    Cannot invoke method. Method invocation is supported only on core types in this language mode.
    At line:1 char:1

    • $oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage

    by the way setx in PowerShell seems to be working but it adds only one directory to the path.

    setx PATH "%PATH%;C:\Apps\test"
    PS H:\> $ENV:PATH

    C:\Program Files (x86)\Microsoft SDKs.........;C:\Program Files\Azure Data Studio\bin;C:\Apps\test

    setx PATH "%PATH%;C:\Apps\testwill_override_previous_set"
    C:\Program Files (x86)\Microsoft SDKs\;C:\Program Files\Azure Data Studio\bin;C:\Apps*testwill_override_previous_set*

    wis there a way to save the PATH in powershell ?

    0 comments No comments

  3. simo2000 1 Reputation point
    2022-06-29T08:42:25.13+00:00

    use setx command only in powershell. don't use it in command line.

    another option is to modify user env variables, by running this command.

    rundll32 sysdm.cpl,EditEnvironmentVariables

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.