Getting user account name in Windows via automated batch script

Discussion in 'Scripting' started by nima1024, Jan 18, 2020.

  1. nima1024

    nima1024 MDL Member

    Sep 18, 2010
    240
    42
    10
    Hello all,
    I'm new to writing scripts in batch, I have encountered a problem, hope you can help me guys.

    I want to know how can I get user account name and then replace it in another command in fully automated script, let me explain the issue by some part of my real script.

    Code:
    COPY C:\Users\nima\AppData\Local\Temp\file.txt C:\file.txt
    
    This script will only work if run under 'nima' user account, how can I get name of user account which run the script and then replace it in that address?

    P.s.: Do I need Administrator privilege to access files in the \AppData\Local\Temp folder or not?
    If yes, I would be really really appreciated I you share 'auto administrator elevation' script to put it at start.

    Best Regards,
    Nima
     
  2. nima1024

    nima1024 MDL Member

    Sep 18, 2010
    240
    42
    10
  3. Windows_Addict

    Windows_Addict MDL Expert

    Jul 19, 2018
    1,245
    3,407
    60
    #3 Windows_Addict, Jan 18, 2020
    Last edited: Jan 18, 2020
    Try,
    Code:
    copy "%temp%\file.txt" "%systemdrive%\file.txt"
    
    It does not need admin privileges. (edit - copying to root of c drive will require admin rights)

    Edit-

    Better,
    Code:
    if exist "%temp%\file.txt" (
    copy "%temp%\file.txt" "%systemdrive%\file.txt"
    )
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. nima1024

    nima1024 MDL Member

    Sep 18, 2010
    240
    42
    10
    My final file must be copied in root of c and I can not ignore that, actually. o_O
     
  5. Windows_Addict

    Windows_Addict MDL Expert

    Jul 19, 2018
    1,245
    3,407
    60
    Try @BAU 's this code to auto elevate batch script (for rights to copy in root of c drive) , https://pastebin.com/XTPt0JSC
    Code:
    ::AveYo: self-elevate passing args and preventing loop
    set "args="%~f0" %*" & call set "args=%%args:"=\""%%"
    reg query HKU\S-1-5-19>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)
    It is good enough, but it's not perfect. A perfect code requires some more lines, but that should be used when other codes in the script also have the same level of treatment.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. nima1024

    nima1024 MDL Member

    Sep 18, 2010
    240
    42
    10
    Thanks!

    Very good point you mentioned.
    In my script, I have to call an EXE that needs access to modify some data in registry, I think I need to run that as Admin, too.
     
  7. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    Adding to the good information here, user name is simply %username%
    C:\Users\%username%\...
    it's one of the system variables like %systemdrive% etc.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,962
    904
    60
    Adding to the post above, you could also use %userprofile% which in my case is "C:\Users\Robert".
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    7,897
    10,733
    240
    Hello although it is out of topic your links are very useful mainly the last good job thanks:)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    #10 Thomas Dubreuil, Jan 20, 2020
    Last edited: Jan 20, 2020
    C:\Users\nima\AppData\Local\Temp
    could be written:
    %systemdrive%\Users\nima\AppData\Local\Temp
    C:\Users\%username%\AppData\Local\Temp
    %userprofile%\AppData\Local\Temp
    %localappdata%\Temp
    %Temp% or %TMP%

    ps: Personally when writing "public scripts", I now prefer to define the temp path I will use as a variable in the beginning of script, because some people will mess with their variables and/or TEMP folder.
    Then using %tmpfolder% variable in script makes it more "bulletproof"
    like this:
    Code:
    set "tmpfolder=C:\Users\%username%\AppData\Local\Temp"
    COPY "%tmpfolder%\file.txt" "C:\file.txt"
    But obviously everyone does have different style and preferences.

    ps2: Using "" is recommended in case there is space in your path (for example my username has a space...)

    ps3: I prefer using robocopy when possible (especially if multiple files), copy is good way to change file name though.

    ps4 :) As an additional note, switches are your friends:
    for example copy /y : suppresses prompting to confirm that you want to overwrite an existing destination file.
    https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy
    (I most often do write copy /b /v /y )
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. nima1024

    nima1024 MDL Member

    Sep 18, 2010
    240
    42
    10