Link to home
Start Free TrialLog in
Avatar of shmitchell22602
shmitchell22602Flag for United States of America

asked on

How to get vbs script to return current username

I need to write a script that will copy a file from a network share to the "C:\Documents and Settings\" & name "\Application Data\Microsoft\Templates\" folder. (name being the current user logged onto the machine).  I have been unable to succefully pull the username.  Any ideas?
Avatar of rettiseert
rettiseert

Hi, look at this example:

Option Explicit

Dim Text, Title
Dim WshNetwork         ' Object variable

Text = "Networking information" & vbCrLf & vbCrLf
Title = "WSH sample - by G. Born"

' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")

Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf

MsgBox Text, vbOKOnly + vbInformation, Title
You could just use the following command and the USERNAME environment variable:

copy "\\servername\share\dir\" "C:\Documents and Settings\%username%\Application Data\Microsoft\Templates\"
Avatar of RobSampson
Hi, you can also use the AppData environment variable from VBScript, shown in DOS by typing
echo %appdata%.

Set objShell = CreateObject("WScript.Shell")
strFolder = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Templates\"
MsgBox strFolder

Regards,

Rob.
ASKER CERTIFIED SOLUTION
Avatar of MeCanHelp
MeCanHelp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FYI:  the code sited above that uses the "MyText" variable didn't work for me.

I had a slightly different task in mind in looking at this post:  I needed to create a VB script for a user so they can delete their Java cache on one of our terminal servers.  Through the power of Google, I found this bit of code that can be used to define the UserName variable:

     Set objWshShell = WScript.CreateObject("WScript.Shell")
     UserName = objWshShell.Environment("PROCESS")("UserName")

Then, I added those 2 lines & the UserName variable to my script to delete all subfolders of the Java Cache folder located at:  "C:\Documents and Settings\%UserName%\Application Data\Sun\Java\Deployment\cache":

*************************************************************
Set objWshShell = WScript.CreateObject("WScript.Shell")
UserName = objWshShell.Environment("PROCESS")("UserName")

Profile = "c:\Documents and Settings\"

Set fs = CreateObject("Scripting.FileSystemObject")

set RootFld = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\cache\")
Set oSub = RootFld.SubFolders

For Each cSubFld In oSub
    Set oSub2 = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\cache\" & cSubFld.Name)
    oSub2.Delete
Next

For Each cFile In RootFld.Files
    cFile.Delete
Next

wscript.echo "Your Java cache folder has been cleared."
*************************************************************

What I came up worked perfectly for me & I will put a copy out there for users who are having issues with Java-based software.
From command line you can access the WMI -

wmic /node:NODENAME computersystem get username
The solution that worked for me was in:
https://www.experts-exchange.com/questions/23766279/How-to-get-the-currently-logged-in-users-Active-Directory-Attribute.html

Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
WScript.Echo objUser.sAMAccountName

To get a listing of all the attributes, use a commandline tool called adfind
http://www.joeware.net/freetools/