Use of environment variable for username in a shortcut (Win 10-1909)

Page 1 of 4 123 ... LastLast

  1. Posts : 110
    Windows 10 Pro 64
       #1

    Use of environment variable for username in a shortcut (Win 10-1909)


    I can run a .bat file with a path such as "C:\Users\Stan\Desktop\PosnPushAlert.lnk"
    But if I try to use "C:\Users\%USERNAME%\Desktop\PosnPushAlert.lnk"
    it fails.
    If I run the .bat file directly instead of from a shortcut, then using the environment variable works. I'm using the shortcut to keep the window hidden.
      My Computer


  2. Posts : 2,799
    Linux Mint 20.1 Win10Prox64
       #2

    use: %USERPROFILE%\Desktop

    Here's a nice little tool for you: SpecialFoldersView - Special Folders Viewer
    Double click to access all special system folders. Right click to copy the environment variable then you can paste into your code.
      My Computer


  3. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #3

    topgundcp said:
    use: %USERPROFILE%\Desktop
    C:\Users\%userprofile%\Desktop...
    does not work.

    nor does %UserProfile%\Desktop\...
      My Computer


  4. Posts : 4,224
    Windows 10
       #4

    Something interesting may be up. What output do you get when you type "set" at the command line. It should show all environment variables. When I type "echo %userprofile%" in cmd.exe I get the same value that shows for USERPROFILE in the general "set" output. Hence my assertion that something odd or interesting might be up.
    HTH,
    --Ed--

    PS: This doesn't work in PowerShell. It only works in cmd. exe. In PowerShell you must use $env:userprofile to reference that value.
      My Computers


  5. Posts : 14,738
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #5

    This is what I get using set at the prompt in a Command Prompt window:
    ALLUSERSPROFILE=C:\ProgramData
    APPDATA=C:\Users\Bert\AppData\Roaming
    BURN_AUTOPLAY=C:\Program Files (x86)\Roxio Easy CD & DVD Burning\Roxio Burn\
    CommonProgramFiles=C:\Program Files\Common Files
    CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
    CommonProgramW6432=C:\Program Files\Common Files
    COMPUTERNAME=DELL15R
    ComSpec=C:\WINDOWS\system32\cmd.exe
    DriverData=C:\Windows\System32\Drivers\DriverData
    EMC_AUTOPLAY=C:\Program Files (x86)\Common Files\Roxio Shared\
    FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
    FPS_BROWSER_USER_PROFILE_STRING=Default
    FP_NO_HOST_CHECK=NO
    HOMEDRIVE=C:
    HOMEPATH=\Users\Bert
    LOCALAPPDATA=C:\Users\Bert\AppData\Local
    LOGONSERVER=\\DELL15R
    NUMBER_OF_PROCESSORS=4
    OneDrive=C:\Users\Bert\OneDrive
    OneDriveConsumer=C:\Users\Bert\OneDrive
    OS=Windows_NT
    Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Progra m Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    PROCESSOR_ARCHITECTURE=AMD64
    PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
    PROCESSOR_LEVEL=6
    PROCESSOR_REVISION=4501
    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    ProgramW6432=C:\Program Files
    PROMPT=$P$G
    PSModulePath=C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
    PUBLIC=C:\Users\Public
    RCAUTOPLAY=C:\Program Files (x86)\Roxio Easy CD & DVD Burning\Roxio Central\
    SESSIONNAME=Console
    SynaProgDir=Synaptics\SynTP
    SystemDrive=C:
    SystemRoot=C:\WINDOWS
    TEMP=C:\Users\Bert\AppData\Local\Temp
    TMP=C:\Users\Bert\AppData\Local\Temp
    USERDOMAIN=DELL15R
    USERDOMAIN_ROAMINGPROFILE=DELL15R
    USERNAME=Bert
    USERPROFILE=C:\Users\Bert
    windir=C:\WINDOWS
      My Computers


  6. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #6

    This is what I get:
    c:\>set
    ALLUSERSPROFILE=C:\ProgramData
    APPDATA=C:\Users\Stan\AppData\Roaming
    CommonProgramFiles=C:\Program Files\Common Files
    CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
    CommonProgramW6432=C:\Program Files\Common Files
    COMPUTERNAME=ASUS7
    ComSpec=C:\WINDOWS\system32\cmd.exe
    DriverData=C:\Windows\System32\Drivers\DriverData
    HOMEDRIVE=C:
    HOMEPATH=\Users\Stan
    LOCALAPPDATA=C:\Users\Stan\AppData\Local
    LOGONSERVER=\\ASUS7
    MAGICK_CODER_MODULE_PATH=C:\Program Files (x86)\File Viewer Lite\lib\magick\modules\coders
    MAGICK_FILTER_MODULE_PATH=C:\Program Files (x86)\File Viewer Lite\lib\magick\modules\filters
    NUMBER_OF_PROCESSORS=4
    OneDrive=C:\Users\Stan\OneDrive
    OS=Windows_NT
    Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v 1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\File Viewer Lite\lib\magick;C:\Program Files\dotnet\;C:\Users\Stan\AppData\Local\Microsoft\WindowsApps;C:\Users\Stan\.dotnet\tools
    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    PROCESSOR_ARCHITECTURE=AMD64
    PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
    PROCESSOR_LEVEL=6
    PROCESSOR_REVISION=5e03
    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    ProgramW6432=C:\Program Files
    PROMPT=$P$G
    PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
    PUBLIC=C:\Users\Public
    SESSIONNAME=Console
    SHTCUT=C:\Users\Stan
    SystemDrive=C:
    SystemRoot=C:\WINDOWS
    TEMP=C:\Users\Stan\AppData\Local\Temp
    TMP=C:\Users\Stan\AppData\Local\Temp
    USERDOMAIN=ASUS7
    USERDOMAIN_ROAMINGPROFILE=ASUS7
    USERNAME=Stan
    USERPROFILE=C:\Users\Stan
    windir=C:\WINDOWS
      My Computer


  7. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #7

    Ok, this is really not all that important. I work on two side by side desktops and I thought it would be nice if I could use the same code on both machines without having to change the username, and as long as it's a conventional .bat file, no problem, I can use an environment variable. But, when I try to do this with a shortcut it always fails. Time to put this baby to bed.
      My Computer


  8. Posts : 72
    Win 10 Pro 19043.1237 (X64) (21H1)
       #8

    Perhaps this might not help but if you put in %USERPROFILE%\Desktop in the Run box, does it open File Explorer to the Desktop folder ?
      My Computer


  9. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #9

    BubbaW said:
    Perhaps this might not help but if you put in %USERPROFILE%\Desktop in the Run box, does it open File Explorer to the Desktop folder ?
    Yes, it does.
      My Computer


  10. Posts : 18,032
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #10

    EdTittel said:
    Something interesting may be up. What output do you get when you type "set" at the command line. It should show all environment variables. When I type "echo %userprofile%" in cmd.exe I get the same value that shows for USERPROFILE in the general "set" output. Hence my assertion that something odd or interesting might be up.
    HTH,
    --Ed--

    PS: This doesn't work in PowerShell. It only works in cmd. exe. In PowerShell you must use $env:userprofile to reference that value.

    Just for information [ I did some testing ].

    When the command Set is run from within a Batch Script, the output includes ANY Variables set within that script.

    The same also applies to a PS Script when Get-Childitem -Path Env: is run from within a PS1 Script.
    Last edited by Paul Black; 06 Mar 2021 at 12:40.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 18:51.
Find Us




Windows 10 Forums