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

Page 2 of 4 FirstFirst 1234 LastLast

  1. Posts : 7,631
    Windows 10 Home 20H2
       #11

    Atom1 said:
    I'm using the shortcut to keep the window hidden.
    Use either of the following lines in a VBScript file, and you will see no window.

    CreateObject("wscript.shell").run("""C:\Users\Stan\Desktop\PosnPushAlert.lnk""")

    CreateObject("wscript.shell").run("""C:\Users\%USERNAME%\Desktop\PosnPushAlert.lnk""")

    Atom1 said:
    But if I try to use "C:\Users\%USERNAME%\Desktop\PosnPushAlert.lnk"
    it fails.
    Try the following line:

    Start "" "C:\Users\%USERNAME%\Desktop\PosnPushAlert.lnk"
      My Computer


  2. Posts : 17,099
    Windows 10 Home x64 Version 22H2 Build 19045.5371
       #12

    Atom1 said:
    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.

    It should work. I do it all the time so I can have the same version of each of my scripts on all my computers.
    1 Please post the batch file code so that the problem can be identified & remedied.
    2 Please post the Target field of the shortcut's properties.
    3 Please state whether or not the batch file or the shortcut are run as Admin.

    Atom1 said:
    I'm using the shortcut to keep the window hidden.
    4 Shortcuts do not have that ability. Please explain exactly what you are doing.

    Denis
      My Computer


  3. Posts : 7,631
    Windows 10 Home 20H2
       #13

    Is "PosnPushAlert" a pop-up message box or a toast notification or a sound alert?
      My Computer


  4. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #14

    Matthew Wai said:
    Is "PosnPushAlert" a pop-up message box or a toast notification or a sound alert?
    It's this:
    Code:
    Powershell.exe -executionpolicy remotesigned -File C:\Users\%UserName%\Desktop\PushOverPS.ps1 "03/05/21|03:58:13 This is a test. "
    - - - Updated - - -

    Try3 said:
    [/COLOR]
    It should work. I do it all the time so I can have the same version of each of my scripts on all my computers.
    1 Please post the batch file code so that the problem can be identified & remedied.
    2 Please post the Target field of the shortcut's properties.
    3 Please state whether or not the batch file or the shortcut are run as Admin.


    4 Shortcuts do not have that ability. Please explain exactly what you are doing.

    Denis
    An application creates a .bat file which in turn runs a PowerShell script, and passes information to the PS script as a parameter.
    The application then causes the .bat file to run.
    The PowerShell script sends a push alert to my phone.
    I started using a shortcut to run the .bat file so I would not see a command window opening, even if only momentarily.
    I then tried to use an environment variable in the shortcut so I could use the same shortcut on any machine. That fails.
    Everything is run as admin.
    Use of environment variable for username in a shortcut  (Win 10-1909)-shortcut.png
      My Computer


  5. Posts : 17,099
    Windows 10 Home x64 Version 22H2 Build 19045.5371
       #15

    In the code you posted, PowerShell does not know what %UserName% means.
    The PS equivalent is $env:username

    1 Is the code you posted your batch file?
    5 If so, what is in PushOverPS.ps1?
    Please answer my questions 2 & 3.

    Denis
      My Computer


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

    Try3 said:
    In the code you posted, PowerShell does not know what %UserName% means.
    The PS equivalent is $env:username


    1 Is the code you posted your batch file?
    5 If so, what is in PushOverPS.ps1?
    Please answer my questions 2 & 3.

    Denis
    This is the .bat file:
    Code:
    Powershell.exe -executionpolicy remotesigned -File C:\Users\%UserName%\Desktop\PushOverPS.ps1 "03/05/21|03:58:13 This is a test. "
    This is the PowerShell script (facsimile)

    Code:
    $From = "me@gmail.com"$To = "you@gmail.com"$Cc = "everybody@gmail.com"$Subject = "This is the subject"$Body = $args[0]$SMTPServer = "mail.mymailserver.com"$username = "MyUsername"$password = "MyUserPassword" | ConvertTo-SecureString -AsPlainText -Force$credential = New-Object System.Management.Automation.PSCredential($username, $password)$SMTPPort = "587"Send-MailMessage -ErrorAction Stop -from "$From" -to "$To" -subject "$Subject" -body "$Body" -SmtpServer "$SMTPserver" -Priority  "Normal" -Credential $credential -Port $SMTPPort -UseSsl
    And all of this works fine, even when I run the .bat file from the shortcut. The only fail is when I try to use an environment variable for the user name in the shortcut.
      My Computer


  7. Posts : 1,208
    11 Home
       #17

    Environment variables entered in the Target field of a shortcut's properties window are automatically expanded during creation of the shortcut. You could work around this problem as described here:
    windows 7 - Environment variable in .lnk shortcut - Super User

    However, you could just paste the code below to a .vbs file and use that instead of your batchfile. It will run your PowerShell command in an invisible PowerShell window.
    Code:
    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run "Powershell.exe -executionpolicy remotesigned -File ""%USERPROFILE%\Desktop\PushOverPS.ps1"" ""03/05/21|03:58:13 This is a test.""", 0
    Set wshShell = Nothing
      My Computers


  8. Posts : 17,099
    Windows 10 Home x64 Version 22H2 Build 19045.5371
       #18

    Atom1 said:
    And all of this works fine, even when I run the .bat file from the shortcut. The only fail is when I try to use an environment variable for the user name in the shortcut.
    The shortcut for which you posted the Target field does not contain any reference to the UserName.

    but you are saying that launching the batch file from the shortcut nevertheless changes how the batch file behaves.

    I suggest a test. Add Echo to the start of the batch file & add a Pause line after its existing contents. Then launch it directly and using the shortcut. Look for any difference in the responses
    Code:
    Echo Powershell.exe -executionpolicy remotesigned -File C:\Users\%UserName%\Desktop\PushOverPS.ps1 "03/05/21|03:58:13 This is a test. "
    Pause to look for differences in the responses

    Then try this for the ps1 file as well so you can see how it is responding.

    Denis
      My Computer


  9. Posts : 7,631
    Windows 10 Home 20H2
       #19

    One line of VBScript.
    Code:
    CreateObject("Wscript.shell").Run("PowerShell -executionpolicy remotesigned -file ""C:\Users\%UserName%\Desktop\PushOverPS.ps1"" ""03/05/21|03:58:13 This is a test. """),0
      My Computer


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

    Try3 said:
    The shortcut for which you posted the Target field does not contain any reference to the UserName.

    but you are saying that launching the batch file from the shortcut nevertheless changes how the batch file behaves.

    I suggest a test. Add Echo to the start of the batch file & add a Pause line after its existing contents. Then launch it directly and using the shortcut. Look for any difference in the responses
    Code:
    Echo Powershell.exe -executionpolicy remotesigned -File C:\Users\%UserName%\Desktop\PushOverPS.ps1 "03/05/21|03:58:13 This is a test. "
    Pause to look for differences in the responses

    Then try this for the ps1 file as well so you can see how it is responding.

    Denis
    Sorry, I was not clear about where the shortcut-username problem occurs.
    This is from my application:
    string BatFileShtCut("C:\Users\Stan\Desktop\PosnPushAlert.lnk");
    I am here defining the path for the shortcut file. If I replace "Stan" with %username%, it does not work.
      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 07:10.
Find Us




Windows 10 Forums